Open In App

LINQ | Sorting Operator | OrderByDescending

Improve
Improve
Like Article
Like
Save
Share
Report

In LINQ, sorting operators are used to rearrange the given sequence in ascending or descending order based on one or more attributes. There are 5 different types of sorting operators are available in LINQ:

  1. OrderBy
  2. OrderByDescending
  3. ThenBy
  4. ThenByDescending
  5. Reverse

OrderByDescending Operator

OrderByDescending operator is used to rearranging the elements of the given sequence in descending order. It does not support query syntax in C# and VB.Net. It only supports method syntax. If you want to rearrange or sort the elements of the given sequence or collection in descending order in query syntax, then use descending keyword as shown in below example.
And in method syntax, use OrderByDescending () method to sort the elements of the given sequence or collection. This method is present in both the Queryable and Enumerable class. And the method syntax is supported by both C# and VB.Net languages. As shown in the below examples. The OrderByDescending method sorts the elements of the collection according to a single property, you are not allowed to sort the collection using multiple properties.

Example 1:




// C# program to print the employee
// ID in descending order
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
  
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                       emp_hire_date = "12/3/2017", emp_salary = 20000},
  
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
  
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
  
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to print the ID of the
        // employees in descending order
        // Using Descending keyword in 
        // query syntax
        var res = from e in emp
                  orderby e.emp_id descending
                  select e;
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee ID: {0}", val.emp_id);
        }
    }
}


Output:

Employee ID: 214
Employee ID: 213
Employee ID: 212
Employee ID: 211
Employee ID: 210
Employee ID: 209

Example 2:




// C# program to print the employee
// name in descending order
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
  
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                    emp_hire_date = "12/3/2017", emp_salary = 20000},
  
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
  
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
  
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",
                                  emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to print the name of the
        // employees in descending order
        // Using OrderByDescending method
        var res = emp.OrderByDescending(e => e.emp_name);
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee Name: {0}", val.emp_name);
        }
    }
}


Output:

Employee Name: Supriya
Employee Name: Soniya
Employee Name: Rohit
Employee Name: Anju
Employee Name: Anjita
Employee Name: Anil


Last Updated : 22 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads