C#: How do I sort a generic list?
There are two simple methods available: OrderBy/OrderByDescending and Sort. The one you choose depends on your intended outcome.
If you want 'in place' sorting (ie: within the existing object), then use
myItems.Sort((x,y) => string.Compare(x.MyProperty, y.MyProperty));
If you want to assign the outcome to a new variable, the use
var mySortedResults = myItems.OrderBy(x=> x.MyProperty).ToList()
If you want 'in place' sorting (ie: within the existing object), then use
myItems.Sort((x,y) => string.Compare(x.MyProperty, y.MyProperty));
If you want to assign the outcome to a new variable, the use
var mySortedResults = myItems.OrderBy(x=> x.MyProperty).ToList()
Comments
Post a Comment