c# list排序

时间:2022-03-03 09:23:20

List<int> tmp = new List<int>(){5,1,22,11,4};

升序:tmp.Sort((x, y) => x.CompareTo(y));

降序:tmp.Sort((x, y) => -x.CompareTo(y));

List<Student> students = new List<Student>();

students.Add(new Student("001","kenshincui",25));

students.Add(new Student("002", "miaoer", 23));

students.Add(new Student("003", "shenjinjuan", 22));

students.Add(new Student("004", "nieyanxin", 24));

升序:students .Sort((x, y) => x.Age.CompareTo(y.Age));

降序:students .Sort((x, y) => -x.Age.CompareTo(y.Age));