Monday, February 19, 2018

c# - How to Sort a List by a property in the object



I have a class called Order which has properties such as OrderId, OrderDate, Quantity, and Total. I have a list of this Order class:




List objListOrder = new List();
GetOrderList(objListOrder); // fill list of orders


Now I want to sort the list based on one property of the Order object, for example I need to sort it by the order date or order id.



How can i do this in C#?


Answer



The easiest way I can think of is to use Linq:




List SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...