Computer >> Máy Tính >  >> Lập trình >> C#

Sắp xếp các phần tử danh sách theo thứ tự giảm dần trong C #

Sau đây là danh sách của chúng tôi với các phần tử -

IList<Employee> emp = new List<Employee>() {
   new Employee() { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 } ,
   new Employee() { EmployeeRank = 05, EmpName = "Raman", EmpMarks = 95 }
};

Bây giờ, hãy sử dụng sắp xếp theo thứ tự và giảm dần để sắp xếp các phần tử theo thứ tự giảm dần.

var res = from str in emp orderby str.EmpName descending select str;

Ví dụ

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      IList<Employee> emp = new List<Employee>() {
         new Employee() { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 } ,
         new Employee() { EmployeeRank = 05, EmpName = "Raman", EmpMarks = 95 }
      };
      var res = from str in emp orderby str.EmpName descending select str;
      Console.WriteLine("Student List (Descending Order):");
      foreach (var list in res)
      Console.WriteLine(list.EmpName);
   }
}

public class Employee {
   public int EmployeeRank { get; set; }
   public string EmpName { get; set; }
   public int EmpMarks { get; set; }
}

Đầu ra

Student List (Descending Order):
Raman
Amit