Exemplo n.º 1
0
    public static void Main()
    {
        //list with students
        var students = new Student[]
        {
            new Student("John", "Doe"),
            new Student("Jane", "Doe"),
            new Student("Alana", "Smith"),
            new Student("Brian", "Jones"),
            new Student("Zoe", "Ferguson"),
            new Student("Bob", "Bobson"),
            new Student("Kate", "Morris")
        };

        Console.WriteLine("List of students: ");
        Console.WriteLine(new string('-', 30));
        foreach (var student in students)
        {
            Console.WriteLine(student.FirstName + " " + student.LastName);
        }

        var selectedStrudents = SortStudentsByName(students);
        Console.WriteLine();
        Console.WriteLine("List of students, whose first name is before their last name: ");
        Console.WriteLine(new string('-', 30));
        foreach (var student in selectedStrudents)
        {
            Console.WriteLine(student.FirstName + " " + student.LastName);
        }
    }
Exemplo n.º 2
0
    private static IEnumerable<Student> SortStudentsByName(Student[] students)
    {
        var selectedStudents =
        from student in students
        where student.FirstName.CompareTo(student.LastName) < 0
        select student;

        return selectedStudents;
    }
Exemplo n.º 3
0
    static void Main()
    {
        //list with students
        var students = new Student[]
        {
            new Student("John", "Doe"),
            new Student("Jane", "Doe"),
            new Student("Alana", "Smith"),
            new Student("Brian", "Jones"),
            new Student("Zoe", "Ferguson"),
            new Student("Bob", "Bobson"),
            new Student("Kate", "Morris")
        };

        //sorting with OrderBy() and ThenBy()
        var lambdaSort = students.OrderByDescending(s => s.FirstName).ThenByDescending(s => s.LastName);
        
        Console.WriteLine("Students sorted with lambda expressions: ");
        Console.WriteLine(new string('-', 30));
        foreach (var student in lambdaSort)
        {
            Console.WriteLine(student.FirstName + " " + student.LastName);
        }

        //sorting with LINQ
        var linqSort =
            from student in students
            orderby student.FirstName descending, student.LastName descending
            select student;

        Console.WriteLine();
        Console.WriteLine("Students sorted with LINQ: ");
        Console.WriteLine(new string('-', 30));
        foreach (var student in linqSort)
        {
            Console.WriteLine(student.FirstName + " " + student.LastName);
        }

    }