Sorting a Generic List By Object Properties

I found this the other day.  When you need to quickly sort a generic list by some property, this is MUCH simpler then using Comparers.

     List<Person> people = repository.GetAll();
     people = people.OrderBy(x => x.LastName).ToList();

or

     List<Person> people = repository.GetAll();
     people = people.OrderBy(x => x.LastName).ThenBy(x => x.FirstName).ToList();
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Reddit
  • DotNetKicks
  • Technorati
  • TwitThis

Leave a Reply