Sorting collections using LINQ

We can sort collections using Linq using OrderBy and OrderByDescending Clause, these extention methods helps to sort collections in Ascending and Descending modes respectively.

For ASCENDING mode.

//Ordering the Persons, order by first name ASC
IOrderedEnumerable<Person> ascPersons = persons.OrderBy(p => p.FirstName);

And for DESCENDING mode

//Ordering the Persons, order by first name DESC
IOrderedEnumerable<Person> descPersons = persons.OrderByDescending(p => p.FirstName);

Happy Coding :)

This entry was posted in .Net, .Net 3.0 / 3.5 and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>