Apply Multiple Order by on a list using LINQ
Introduction
Normally while working on a project, I avoid the use of LINQ query and prefer to do all the grouping and ordering operation in the SQL procedure itself but recently, I came across a situation where I need to perform multiple order by operation on a list i.e. order by multiple columns.In this article, I will explain how to apply multiple order by on a list.
Sample example
To order a list in LINQ, we use the order by function. Multiple order by in linq is performed using orderby function followed by the ThenBy function. To demonstrate how to use multiple orderby, let us consider the following data in a list
List<Data> crics = new List<Data>();
public class Data
{
public string FirstName get; set;
public string LastName get; set;
public int Age get; set;
public bool Available get; set;
public DateTime DOJ get; set;
}
public void LoadGrid()
{
crics.Add(new Data()
{
FirstName = "Mahi",
LastName = "Dhoni",
Age = 31,
Available = false,
DOJ = new DateTime(2001, 11, 10)
});
crics.Add(new Data()
{
FirstName = "Sachin",
LastName = "Mall",
Age = 29,
Available = true,
DOJ = new DateTime(2011, 05, 07)
});
crics.Add(new Data()
{
FirstName = "Sachin",
LastName = "Tendulkar",
Age = 39,
Available = true,
DOJ = new DateTime(1989, 01, 08)
});
}
Now I can apply multiple order by using the following code.
var filteredCrics = crics.OrderBy(c => c.FirstName).ThenBy(n => n.LastName);
In the above piece of code, I have ordered the list first by FirstName and then by LastName. Now adding some more data, if I bind this ordered data in a list then I can see the output in the grid as
Output:
As you see in the above screenshot, data is first sorted by first name and then by last name in ascending order.
Order by Descending order
Ascending order is fine but what about descending order? What if I want to apply order by first in ascending order and then in descending order? To use the combination of ascending and descending, use the following query:
var filteredCrics = from row in crics
orderby row.Age descending, row.DOJ
select row;
Above query will first sort data in descending order of age followed by ascending order of DOJ. Binding the above query on a grid, I get the output screen as
As you see in the above screenshot, first the data is sorted by age in descending order and then by date of joining in ascending order. Since there is no duplicate age, order by DOJ is of no significance here.
Is it possible to use three order by condition in a single statement?
Yes it is indeed possible. Infact, you can use any number of order by in a single statement. Below query sort the list in ascending order of FirstName followed by descending order of LastName followed by ascending order of age.
var filteredCrics = crics.OrderBy(fn => fn.FirstName).ThenByDescending(ln => ln.LastName).ThenBy(age => age.Age);
On binding the above sorted data to a grid, I get the output as:
As you see in the above screenshot, list is first sorted by FirstName and then by LastName and finally by Age. As you see in the above screen, third order by is of significance here and was used just for the sake of explanation.
Conclusion:
In this article, we saw how to apply multiple order by on a list and then bind the list to a datagrid. Please find the attached file for sample code.