Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Sắp xếp danh sách giảm dần trong C#

Ví dụ trùng lặp với bài 1003091780 - sắp xếp List<Employee> giảm dần theo tên sử dụng query syntax.

Mã nguồn

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 = 5, EmpName = "Raman", EmpMarks = 95 }
        };

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

        Console.WriteLine("Danh sách sinh viên (Giảm dần):");
        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; }
}

Kết quả đầu ra

Danh sách sinh viên (Giảm dần):
Raman
Amit

Lưu ý: Bài này trùng lặp nội dung với ID 1003091780. Trong thực tế nên gộp hoặc xóa bài trùng.