How to sort a List collection of classes with properties in them?

前端 未结 3 383
误落风尘
误落风尘 2020-12-16 03:30

I have a List of classes in my collection like

List test = new List();

In my classes I have just some propert

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 04:13

    If you want to sort them in place, you can use List.Sort:

    test.Sort( (l,r) => l.date.CompareTo(r.date) );
    

    If you want to sort them into a new result set, LINQ is very nice:

    var sortedResults = from mc in test
                        order by mc.date
                        select mc;
    

提交回复
热议问题