Multiple Sort For Collection Using LINQ

There are quite a lot of instance where you need to do a primary sort and you need a secondary sort on the same collection and sometimes even multiple sort. With LINQ we can achieve it pretty straight forward.

Let us assume the situation here. We have a list of employees and we need the output to be sorted by city and within every city, we need it to be sorted by the employee’s first name. Below is the code which will get your job done.

employeeList.OrderBy(item => item.FirstName).ThenBy(item => item.City);

Remember the inner sort / secondary sort has to appear first and the outer sort at the last.

Multiple Sort For Collection Using LINQ

Leave a Reply

Scroll to top
%d bloggers like this: