Ngôn ngữ LINQ - Toán tử Reverse

Toán tử sắp xếp Reverse trong LINQ

Phương thức mở rộng Reverse sắp xếp danh sách theo thứ tự ngược lại và nó chỉ hỗ trợ cú pháp phương thức LINQ.

Ví dụ dưới đây minh họa cách sử dụng toán tử sắp xếp Reverse trong LINQ:

// Student collection
IList<Student> studentList = new List<Student>() 
{ 
	new Student() { StudentID = 1, StudentName = "John", Age = 18 },
	new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 },
	new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 },
	new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 },
	new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 } 
};

var multiSortingResult = studentList.Reverse().ToList();

foreach (var std in multiSortingResult)
   Console.WriteLine("Name: {0}, Age:{1}", std.StudentName, std.Age);

Đây là kết quả khi biên dịch và thực thi chương trình:

Name: Ron, Age 19
Name: Ram, Age 20
Name: Bill, Age 25
Name: Steve, Age 15
Name: John, Age 18

Xem ví dụ


Sắp xếp trên nhiều trường

Bạn có thể sắp xếp danh sách trên nhiều trường được phân tách bằng dấu phẩy.

Danh sách sẽ được sắp xếp dựa trên trường đầu tiên và sau đó nếu giá trị của trường thứ nhất giống nhau thì nó sẽ sử dụng trường thứ hai để sắp xếp, v.v.

Sử dụng cú pháp truy vấn LINQ

// Student collection
IList<Student> studentList = new List<Student>() 
{ 
    new Student() { StudentID = 1, StudentName = "John", Age = 18 },
    new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 },
    new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 },
    new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 },
    new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 },
    new Student() { StudentID = 6, StudentName = "Ron" , Age = 18 },  
    new Student() { StudentID = 7, StudentName = "Ram" , Age = 14 }
};

var multiSortingResult = from s in studentList
                         orderby s.StudentName, s.Age 
                         select s;

var multiSortingMixResult = from s in studentList
                            orderby s.StudentName ascending, s.Age descending
                            select s;
                             
Console.WriteLine("Result 1:");
foreach (var std in multiSortingResult)
   Console.WriteLine("Name: {0}, Age:{1}", std.StudentName, std.Age);

Console.WriteLine("Result 2:");
foreach (var std in multiSortingMixResult)
  Console.WriteLine("Name: {0}, Age:{1}", std.StudentName, std.Age);

Đây là kết quả khi biên dịch và thực thi chương trình:

Result 1:
Name: Bill, Age 25
Name: John, Age 18
Name: Ram, Age 14
Name: Ram, Age 20
Name: Ron, Age 18
Name: Ron, Age 19
Name: Steve, Age 15
Result 2:
Name: Bill, Age 25
Name: John, Age 18
Name: Ram, Age 20
Name: Ram, Age 14
Name: Ron, Age 19
Name: Ron, Age 18
Name: Steve, Age 15

Xem ví dụ


Sử dụng cú pháp phương thức LINQ

Sắp xếp trên nhiều trường trong cú pháp phương thức khác với cú pháp truy vấn. Nó sử dụng các phương thức mở rộng OrderBy / OrderByDescending để sắp xếp danh sách theo trường đầu tiên và ThenBy / ThenByDescending để sắp xếp danh sách theo trường tiếp theo.

// Student collection
IList<Student> studentList = new List<Student>() 
{ 
    new Student() { StudentID = 1, StudentName = "John", Age = 18 },
    new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 },
    new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 },
    new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 },
    new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 },
    new Student() { StudentID = 6, StudentName = "Ron" , Age = 18 },  
    new Student() { StudentID = 7, StudentName = "Ram" , Age = 14 }
};

var multiSortingResult = studentList.OrderBy(s => s.StudentName)
                                    .ThenBy(s => s.Age)
                                    .ToList();

var multiSortingMixResult = studentList.OrderBy(s => s.StudentName)
                                       .ThenByDescending(s => s.Age)
                                       .ToList();
                             
Console.WriteLine("Result 1:");
foreach (var std in multiSortingResult)
  Console.WriteLine("Name: {0}, Age:{1}", std.StudentName, std.Age);

Console.WriteLine("Result 2:");
foreach (var std in multiSortingMixResult)
  Console.WriteLine("Name: {0}, Age:{1}", std.StudentName, std.Age);

Đây là kết quả khi biên dịch và thực thi chương trình:

Result 1:
Name: Bill, Age 25
Name: John, Age 18
Name: Ram, Age 14
Name: Ram, Age 20
Name: Ron, Age 18
Name: Ron, Age 19
Name: Steve, Age 15
Result 2:
Name: Bill, Age 25
Name: John, Age 18
Name: Ram, Age 20
Name: Ram, Age 14
Name: Ron, Age 19
Name: Ron, Age 18
Name: Steve, Age 15

Xem ví dụ


Những điểm cần nhớ về toán tử sắp xếp của LINQ

  1. Hai phương thức OrderBy hoặc ThenBy mặc định sắp xếp theo thứ tự tăng dần.
  2. Cú pháp truy vấn LINQ không hỗ trợ OrderByDescending, ThenBy, ThenByDescending và Reverse. Nó chỉ hỗ trợ mệnh đề 'Order By' để sắp xếp 'tăng dần' và 'giảm dần'.
  3. Bạn phải sử dụng các phương thức OrderBy hoặc OrderByDescending kết hợp với ThenBy hoặc ThenByDescending để sắp xếp theo nhiều trường khác nhau khi sử dụng cú pháp phương thức LINQ.