patterncsharpCritical
List<T> OrderBy Alphabetical Order
Viewed 0 times
orderbylistalphabeticalorder
Problem
I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic
List. For the sake of this example, let's say I have a List of a Person type with a property of lastname. How would I sort this List using a lambda expression?List people = PopulateList();
people.OrderBy(???? => ?????)Solution
If you mean an in-place sort (i.e. the list is updated):
If you mean a new list:
people.Sort((x, y) => string.Compare(x.LastName, y.LastName));If you mean a new list:
var newList = people.OrderBy(x=>x.LastName).ToList(); // ToList optionalCode Snippets
people.Sort((x, y) => string.Compare(x.LastName, y.LastName));var newList = people.OrderBy(x=>x.LastName).ToList(); // ToList optionalContext
Stack Overflow Q#188141, score: 748
Revisions (0)
No revisions yet.