LINQ | Sorting Operator | ThenByDescending

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

ThenByDescending Operator

The ThenByDescending operator is used to implement secondary sort in descending order. The multiple sorting is supported by ThenByDescending operator. Generally, ThenByDescending method is used with the OrderBy method. The OrderBy() method first sort the elements of the sequence or collection in descending order after that ThenByDescending() method is used to again sort the result of OrderBy() method in descending order. Or in other words, in LINQ, the collection is the first sort according to the primary field which is given by OrderBy method after that the result of the primary sort is again sorted by the secondary field that is given by the ThenByDescending method.

  • It does not support query syntax in C# and VB.Net languages.
  • It support method syntax in both C# and VB.Net languages.
  • It present in both the Queryable and Enumerable class.
  • The secondary sorting in query syntax is separated by using comma.
  • It is implemented by using deferred execution.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print the employees
// name and their salary
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 and
        // salary of the employees
        // Using OrderByDescending and
        // ThenByDescending method
        var res = emp.OrderByDescending(e => 
           e.emp_name).ThenByDescending(e =>
                              e.emp_salary);
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee Name: {0}  Salary: {1}",
                                    val.emp_name, val.emp_salary);
        }
    }
}

chevron_right


Output:

Employee Name: Supriya  Salary: 40000
Employee Name: Soniya  Salary: 30000
Employee Name: Rohit  Salary: 40000
Employee Name: Anju  Salary: 50000
Employee Name: Anjita  Salary: 20000
Employee Name: Anil  Salary: 40000

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print the employees
// IDs and their salary
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 IDs and
        // salary of the employees
        // Using OrderBy and ThenByDescending 
        // method
        var res = emp.OrderBy(e => 
            e.emp_id).ThenByDescending(e => 
                              e.emp_salary);
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee ID: {0} Salary: {1}"
                              , val.emp_id, val.emp_salary);
        }
    }
}

chevron_right


Output:

Employee ID: 209 Salary: 20000
Employee ID: 210 Salary: 30000
Employee ID: 211 Salary: 40000
Employee ID: 212 Salary: 40000
Employee ID: 213 Salary: 40000
Employee ID: 214 Salary: 50000


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.