Author : Rahul
Last Modified : 13-Jul-2021
Complexity : Beginner

Ordering Operator in LINQ


Ordering Operator in LINQ is used to arrange the order of the collection in ascending or descending order based on some attributes. There is five ordering operators available in LINQ as follow:

  1. ORDER BY
  2. ORDER BY DESCENDING
  3. THEN BY
  4. THEN BY DESCENDING
  5. REVERSE

 

ORDERBY Operator

ORDERBY Operator is used to sort the elements of the collection in Ascending order. ORDERBY keyword is used for this purpose. Below is some example in this category.

Query to sort the list of word alphabetically.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "ankit", "gopal", "kartik" };
     
     //Linq Query
     var result_Linq = from p in words orderby p select p;
     
     //Lambda Expression
     var result_Lambda = words.OrderBy(p => p);
     
     //Display Linq Query Result
     Console.WriteLine("Sorted List of Words (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted List of Words (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Sorted List of Words (Using Linq):
ankit
gopal
kartik
rahul
Sorted List of Words (Using Lambda):
ankit
gopal
kartik
rahul

 

Query to sort the list of word alphabetically and case-insensitive.

 public class CaseInsensitiveComparision : IComparer<string>
 {
   public int Compare(string x, string y)
   {
     return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
   }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rAhul", "RaJesh", "GoPal", "gOpiNath" };
     
     //Linq Query
     var result_Linq = words.OrderBy(p => p, new CaseInsensitiveComparision());
     
     //Lambda Expression
     var result_Lambda = words.OrderBy(p => p, new CaseInsensitiveComparision());
     
     //Display Linq Query Result
     Console.WriteLine("Sorted List of Words (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted List of Words (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Sorted List of Words (Using Linq):
GoPal
gOpiNath
rAhul
RaJesh
Sorted List of Words (Using Lambda):
GoPal
gOpiNath
rAhul
RaJesh

 

Query to sort the list of employees by their name.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees orderby p.Emp_Name select p;
     
     //Lambda Expression
     var result_Lambda = employees.OrderBy(p => p.Emp_Name);
     
     //Display Linq Query Result
     Console.WriteLine("Sorted Employee Details (Using Linq):");
     foreach (Employee item in result_Linq)
     {
       Console.WriteLine($"Employee Name: {item.Emp_Name}, Employee Department: {item.Emp_Department}, " +
         $"Employee Salary: {item.Emp_Salary}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted Employee Details (Using Lambda):");
     foreach (Employee item in result_Lambda)
     {
       Console.WriteLine($"Employee Name: {item.Emp_Name}, Employee Department: {item.Emp_Department}, " +
         $"Employee Salary: {item.Emp_Salary}");
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Sorted Employee Details (Using Linq):
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
Sorted Employee Details (Using Lambda):
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000

 

ORDERBYDESCENDING Operator

ORDERBYDESCENDING Operator is used to sort the elements of the collection in Descending order. ORDERBYDESCENDING keyword is used for this purpose. Below is some example in this category.

Query to sort the list of word alphabetically in descending order.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "ankit", "gopal", "kartik" };
     
     //Linq Query
     var result_Linq = from p in words orderby p descending select p;
     
     //Lambda Expression
     var result_Lambda = words.OrderByDescending(p => p);
     
     //Display Linq Query Result
     Console.WriteLine("Sorted List of Words (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted List of Words (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Sorted List of Words (Using Linq):
rahul
kartik
gopal
ankit
Sorted List of Words (Using Lambda):
rahul
kartik
gopal
ankit

 

Query to sort the list of word alphabetically in descending order and case-insensitive.

 public class CaseInsensitiveComparision : IComparer<string>
 {
   public int Compare(string x, string y)
   {
     return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
   }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rAhul", "RaJesh", "GoPal", "gOpiNath" };
     
     //Linq Query
     var result_Linq = words.OrderByDescending(p => p, new CaseInsensitiveComparision());
     
     //Lambda Expression
     var result_Lambda = words.OrderByDescending(p => p, new CaseInsensitiveComparision());
     
     //Display Linq Query Result
     Console.WriteLine("Sorted List of Words (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted List of Words (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Sorted List of Words (Using Linq):
RaJesh
rAhul
gOpiNath
GoPal
Sorted List of Words (Using Lambda):
RaJesh
rAhul
gOpiNath
GoPal

 

Query to sort the list of employees by their name in descending order.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees orderby p.Emp_Name descending select p;
     
     //Lambda Expression
     var result_Lambda = employees.OrderByDescending(p => p.Emp_Name);
     
     //Display Linq Query Result
     Console.WriteLine("Sorted Employee Details (Using Linq):");
     foreach (Employee item in result_Linq)
     {
       Console.WriteLine($"Employee Name: {item.Emp_Name}, Employee Department: {item.Emp_Department}, " +
         $"Employee Salary: {item.Emp_Salary}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted Employee Details (Using Lambda):");
     foreach (Employee item in result_Lambda)
     {
       Console.WriteLine($"Employee Name: {item.Emp_Name}, Employee Department: {item.Emp_Department}, " +
         $"Employee Salary: {item.Emp_Salary}");
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Sorted Employee Details (Using Linq):
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Sorted Employee Details (Using Lambda):
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000

 

THENBY Operator

THENBY Operator is used to sort the elements of the collection in Ascending order if it is not possible to sort the elements of the collection based on the primary sorting attribute. ORDERBY keyword is used for this purpose. Below is some example in this category.

Query to sort the list of word by length and then alphabetically.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "ankit", "gopal", "kartik" };
     
     //Linq Query
     var result_Linq = from p in words orderby p.Length , p select p;
     
     //Lambda Expression
     var result_Lambda = words.OrderBy(p=>p.Length).ThenBy(p => p);
     
     //Display Linq Query Result
     Console.WriteLine("Sorted List of Words (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted List of Words (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Sorted List of Words (Using Linq):
ankit
gopal
rahul
kartik
Sorted List of Words (Using Lambda):
ankit
gopal
rahul
kartik

 

Query to sort the list of employees by its department name and then by its name.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees orderby p.Emp_Department, p.Emp_Name select p;
     
     //Lambda Expression
     var result_Lambda = employees.OrderBy(p=>p.Emp_Department).ThenBy(p => p.Emp_Name);
     
     //Display Linq Query Result
     Console.WriteLine("Sorted Employee Details (Using Linq):");
     foreach (Employee item in result_Linq)
     {
       Console.WriteLine($"Employee Name: {item.Emp_Name}, Employee Department: {item.Emp_Department}, " +
         $"Employee Salary: {item.Emp_Salary}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted Employee Details (Using Lambda):");
     foreach (Employee item in result_Lambda)
     {
       Console.WriteLine($"Employee Name: {item.Emp_Name}, Employee Department: {item.Emp_Department}, " +
         $"Employee Salary: {item.Emp_Salary}");
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Sorted Employee Details (Using Linq):
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
Sorted Employee Details (Using Lambda):
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000

 

Query to sort the list of word by length and then alphabetically and case-insensitive.

 public class CaseInsensitiveComparision : IComparer<string>
 {
   public int Compare(string x, string y)
   {
     return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
   }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rAhul", "RaJesh", "GoPal", "gOpiNath" };
     
     //Linq Query
     var result_Linq = words.OrderBy(p=>p.Length).ThenBy(p => p, new CaseInsensitiveComparision());
     
     //Lambda Expression
     var result_Lambda = words.OrderBy(p=>p.Length).ThenBy(p => p, new CaseInsensitiveComparision());
     
     //Display Linq Query Result
     Console.WriteLine("Sorted List of Words (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted List of Words (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Sorted List of Words (Using Linq):
GoPal
rAhul
RaJesh
gOpiNath
Sorted List of Words (Using Lambda):
GoPal
rAhul
RaJesh
gOpiNath

 

THENBYDESCENDING Operator

THENBY Operator is used to sort the elements of the collection in Descending order if it is not possible to sort the elements of the collection based on the primary sorting attribute. ORDERBYDESCENDING keyword is used for this purpose. Below is some example in this category.

Query to sort the list of word by length and then alphabetically in descending order.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "ankit", "gopal", "kartik" };
     
     //Linq Query
     var result_Linq = from p in words orderby p.Length , p descending select p;
     
     //Lambda Expression
     var result_Lambda = words.OrderBy(p=>p.Length).ThenByDescending(p => p);
     
     //Display Linq Query Result
     Console.WriteLine("Sorted List of Words (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted List of Words (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Sorted List of Words (Using Linq):
rahul
gopal
ankit
kartik
Sorted List of Words (Using Lambda):
rahul
gopal
ankit
kartik

 

Query to sort the list of employees by its department name and then by its name in descending order.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees orderby p.Emp_Department, p.Emp_Name descending select p;
     
     //Lambda Expression
     var result_Lambda = employees.OrderBy(p=>p.Emp_Department).ThenByDescending(p => p.Emp_Name);
     
     //Display Linq Query Result
     Console.WriteLine("Sorted Employee Details (Using Linq):");
     foreach (Employee item in result_Linq)
     {
       Console.WriteLine($"Employee Name: {item.Emp_Name}, Employee Department: {item.Emp_Department}, " +
         $"Employee Salary: {item.Emp_Salary}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Sorted Employee Details (Using Lambda):");
     foreach (Employee item in result_Lambda)
     {
       Console.WriteLine($"Employee Name: {item.Emp_Name}, Employee Department: {item.Emp_Department}, " +
         $"Employee Salary: {item.Emp_Salary}");
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     return employees;
   }
 }

Output

Sorted Employee Details (Using Linq):
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Sorted Employee Details (Using Lambda):
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Name: Amit, Employee Department: HR, Employee Salary: 20000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 40000
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000


REVERSE Operator

REVERSE Operator is used to reverse the order of the elements of the collection. REVERSE keyword is used for this purpose. Below is some example in this category.

Query to return a list of words in reverse order.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rahul", "rajesh", "abhay", "gopal" };
     
     //Linq Query
     var result_Linq = (from p in words select p).Reverse();
     
     //Lambda Expression
     var result_Lambda = words.Reverse();
     
     //Display Original list
     Console.WriteLine("Original List of Words:");
     foreach (var item in words)
     {
       Console.WriteLine(item);
     }
     
     //Display Linq Query Result
     Console.WriteLine("List of Words in reverse order(Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("List of Words in reverse order (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Original List of Words:
rahul
rajesh
abhay
gopal
List of Words in reverse order(Using Linq):
gopal
abhay
rajesh
rahul
List of Words in reverse order (Using Lambda):
gopal
abhay
rajesh
rahul