自定義對象User ,給List<User> 集合按年齡大小排序。 [csharp] public class User { public User(int age, string name) { Age = age; Name = name; } public int Age { get; set; } public string Name { get; set; } } 這裡我們需要一個自定義的比較器 [csharp] public static int CompareByAge(User x, User y)//從大到小排序器 { if (x == null) { if (y == null) { return 0; } return 1; } if (y == null) { return -1; } int retval = y.Age.CompareTo(x.Age); return retval; } 然後就可以使用List<T>.sort(CompareByAge)來排序了 [csharp] List<User> list = new List<User> { new User(14,"aa"), new User(17,"bb"), new User(12,"cc"),new User(33,"dd") }; www.2cto.com foreach (var item in list) { Console.WriteLine(item.Name + ":" + item.Age); } Console.WriteLine("排序後"); list.Sort(CompareByAge); foreach (var item in list) { Console.WriteLine(item.Name+":"+item.Age); } Console.ReadKey();