How to Sort a List by a property in the object

前端 未结 20 2003
醉梦人生
醉梦人生 2020-11-21 08:25

I have a class called Order which has properties such as OrderId, OrderDate, Quantity, and Total. I have a l

20条回答
  •  时光说笑
    2020-11-21 08:35

    If you need to sort the list in-place then you can use the Sort method, passing a Comparison delegate:

    objListOrder.Sort((x, y) => x.OrderDate.CompareTo(y.OrderDate));
    

    If you prefer to create a new, sorted sequence rather than sort in-place then you can use LINQ's OrderBy method, as mentioned in the other answers.

提交回复
热议问题