I have a class called Person which contains a property LastName, which reflects a string cooresponding to the Person’s last name.
I created a List as follows:
var People = List<Person>
What I would like to do is sort the people by their LastName property in alphabetical order.
After looking at some examples, I’ve tried
People = People.OrderBy(p => p.LastName);
But it does not work.
Using LINQ, you’d need to convert the results back into a
List<Person>:Since
OrderByreturns anIOrderedEnumerable<T>, you need the extra callToList()to turn this back into a list.However, since you effectively want an in-place sort, you can also use
List<T>.Sortdirectly: