There are mainly five types of Ordering Operators in Linq.
1. OrderBy
2. OrderByDescending
3. ThenBy
4. ThenByDescending
5. Reverse
1.OrderBy
public 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
2.OrderByDescending
public 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
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
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);
}
}
OutPutSorting By Length then alphabetically in descending order :
Tom
Jay
Mary
Dick
Harry
5.Reverse
public 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);
}
}
OutPutReversing the collection :
Jay
Mary
Harry
Dick
Tom
1. OrderBy
2. OrderByDescending
3. ThenBy
4. ThenByDescending
5. Reverse
1.OrderBy
public 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
2.OrderByDescending
public 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
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
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);
}
}
OutPutSorting By Length then alphabetically in descending order :
Tom
Jay
Mary
Dick
Harry
5.Reverse
public 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);
}
}
OutPutReversing the collection :
Jay
Mary
Harry
Dick
Tom
No comments:
Post a Comment