LINQ Sorting Operator

In LINQ, Sorting Operators are extremely helpful to alter the order of the data (mostly either ascending or descending) based on one or more fields. The following are the different types of sorting operators available in LINQ.

Topics

  • ORDER BY
  • ORDER BY DESCENDING
  • THEN BY
  • THEN BY DESCENDING
  • REVERSE

LINQ OrderBy Operator

OrderBy Operator can be extremely useful to sort the values of a list/Collection in ascending order.

Sum=my_Datasource.OrderBy(“Expression”)

LINQ OrderBy Operator Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
class Program
{
static void Main(string[] args)
{
string[] food_Items = { "Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton" };
//Trying to select the items in ascending it...
IEnumerable<string> result = food_Items.OrderBy(x => x);
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ReadLine(); 
}
}
}
Output:
Butter Chicken
Chicken 65
Chicken tikka
Fish
Mutton

LINQ OrderBy Operator Example using Vb.Net

Module Module1
 Sub Main()

  Dim food_List As String() = {"Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton"}
  Dim result As IEnumerable(Of String) = food_List.OrderBy(Function(x) x)
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()

 End Sub
End Module
Output:
Butter Chicken
Chicken 65
Chicken tikka
Fish
Mutton

LINQ OrderByDescending Operator

OrderByDescending Operator can be useful to sort the values of a list/Collection in descending order.

Sum=my_Datasource.OrderByDescending(“Expression”)

LINQ OrderByDescending Operator Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
class Program
{
static void Main(string[] args)
{
string[] food_Items = { "Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton" };
//Trying to select the items in descending it...
IEnumerable<string> result = food_Items.OrderByDescending(x => x);
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ReadLine(); 
}
}
}
Output:
Mutton
Fish
Chicken tikka
Chicken 65
Butter Chicken

LINQ OrderByDescending Operator Example using Vb.Net

Module Module1
 Sub Main()

  Dim food_List As String() = {"Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton"}
  Dim result As IEnumerable(Of String) = food_List.OrderByDescending(Function(x) x)
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()

 End Sub
End Module
Output:
Mutton
Fish
Chicken tikka
Chicken 65
Butter Chicken

LINQ Reverse Operator

Reverse Operator can be useful to reverse the values of a list/Collection.

Sum=my_Datasource..Reverse

LINQ Reverse Operator Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] my_List = { "I", "am", "Sharath"};
            //Trying to reverse the items in a list...
            IEnumerable<string> result = my_List.Reverse();
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine(); 
        }
    }
}
Output:
Sharath
am
I

LINQ Reverse Operator Example using Vb.Net

Module Module1
 Sub Main()

  Dim my_List As String() = { "I", "am", "Sharath"}
  Dim result As IEnumerable(Of String) = my_List.Reverse()
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()

 End Sub
End Module
Output:
Sharath
am
I

Previous topic: LINQ Filtering Operator
Next Topic: LINQ Partition Operator

For more articles on LINQ, Click here

 

 

Advertisements

Author: Sharath Raju

Founder@www.ExcelCult.com

Leave a Reply