实现:采用Sort方法根据T类某个字段对泛型集合List<T>排序。
实现代码如下:
static void Main(string[] args)
{
List<Stud> studs = new List<Stud>() {
new Stud() { Name = "B", Score = 80 },
new Stud() { Name = "C", Score = 30 },
new Stud() { Name = "A", Score = 69 },
};
//按成绩排
//studs.Sort((stud1, stud2) => stud1.Score - stud2.Score);
//按名字排
studs.Sort((stud1, stud2) => string.Compare(stud1.Name, stud2.Name));
foreach (var item in studs)
{
Console.WriteLine("Name:{0},Scoe:{1}", item.Name, item.Score);
}
Console.ReadKey();
}
class Stud
{
public string Name { get; set; }
public int Score { get; set; }
}