Ordering Operators in Linq
In this article we will discuss about the Ordering Operators in LINQ. Ordering Operators are used whenever we wants to sort the collections in some specific manner which can be ascending, descending or simply reversing the collection.
Introduction
In this article we will discuss about the Ordering Operators in LINQ.
Ordering Operators are used whenever we wants to sort the collections
in some specific manner which can be ascending, descending or simply
reversing the collection.
There are mainly five types of Ordering Operators in Linq.
1. OrderBy
2. OrderByDescending
3. ThenBy
4. ThenByDescending
5. Reverse
Now lets discuss all of them one by one.Explanation
1. OrderBypublic void OrderBy()
{
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var sortedNames = names.OrderBy (s => s);
var sortedNamesLengthWise = names.OrderBy (s => s.Length);
Console.WriteLine("Sorted List: ");
foreach (var n in sortedNames)
{
Console.WriteLine(n);
}
Console.WriteLine("Sorted List LengthWise: ");
foreach (var nlw in sortedNamesLengthWise)
{
Console.WriteLine(nlw);
}
}
OutPut
Sorted List:
Dick
Harry
Jay
Mary
Tom
Sorted List LengthWise:
Tom
Jay
Dick
Mary
Harry
In the above example we have a string array, now using OrderBy we are sorting it in ascending order. There are two ways shown in this example, the sorting can be done albhabetically or can be done Length wise.
2. OrderByDescendingpublic void OrderByDescending()
{
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var sortedNames = names.OrderByDescending(s => s);
var sortedNamesLengthWise = names.OrderByDescending(s => s.Length);
Console.WriteLine("Sorted Descending : ");
foreach (var n in sortedNames)
{
Console.WriteLine(n);
}
Console.WriteLine("Sorted Descending LengthWise: ");
foreach (var nlw in sortedNamesLengthWise)
{
Console.WriteLine(nlw);
}
}
OutPut
Sorted List:
Tom
Mary
Jay
Harry
Dick
Sorted List LengthWise:
Harry
Dick
Mary
Tom
Jay
In the above example we have a string array, now using OrderByDescending we are sorting it in descending order. The array can be sorted in descending order either albhabetically or can be done Length wise, as shown in the example.
3. ThenBy public void ThenBy()
{
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var sortedNames = names.OrderBy(s => s.Length).ThenBy (s => s);
var sortedNamesLengthWise = names.OrderBy (s => s.Length).ThenBy (s => s[1]).ThenBy (s => s[0]);
Console.WriteLine("Sorting By Length then alphabetically : ");
foreach (var n in sortedNames)
{
Console.WriteLine(n);
}
Console.WriteLine("By length, then second character, then first character : ");
foreach (var nlw in sortedNamesLengthWise)
{
Console.WriteLine(nlw);
}
}
OutPut
Sorting By Length then alphabetically :
Jay
Tom
Dick
Mary
Harry
By length, then second character, then first character :
Jay
Tom
Mary
Dick
Harry
In the above example we have a string array, now here we are using both OrderBy and ThenBy. The example says that we can apply more than one sort order in one query.
The array can be sorted first by OrderBy by applying a condition and after that we can sort that sorted list again by applying some other condition. Like in the above example we are first sorting the array length wise then alphabetically.
4. ThenByDescending public void ThenByDescending()
{
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var sortedNames = names.OrderBy(s => s.Length).ThenByDescending(s => s);
Console.WriteLine("Sorting By Length then alphabetically in descending order : ");
foreach (var n in sortedNames)
{
Console.WriteLine(n);
}
}
OutPut
Sorting By Length then alphabetically in descending order :
Tom
Jay
Mary
Dick
Harry
In the above example we have a string array, now here we are using both OrderBy and ThenByDescending. The above example says that if we have applied a sort order on a collection and then we want to apply a sorting in descending order in the resultant collection then we can do that using ThenByDescending().
5. Reversepublic void Reverse()
{
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var reverse = names.Reverse();
Console.WriteLine("Reversing the collection : ");
foreach (var r in reverse)
{
Console.WriteLine(r);
}
}
OutPut
Reversing the collection :
Jay
Mary
Harry
Dick
Tom
In the above example we are just reversing the collection using Reverse(). The order is just reversed as shown in the above example.Feedback
If you have any queries regarding Ordering Operators, please do ask me.