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()

Comments

Popular posts from this blog

SharePoint 2013: Error updating managed account credentials

Error deploying Nintex workflow: An item with the same key has already been added