Open In App

LINQ | Sorting Operator | ThenBy

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

ThenBy Operator

ThenBy operator is used to implementing secondary sort in ascending order. The multiple sorting is supported by ThenBy operator. Generally, ThenBy method is used with the OrderBy method. The OrderBy() Method, first sort the elements of the sequence or collection in ascending order after that ThenBy() method is used to again sort the result of OrderBy() method in ascending 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 ThenBy 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 as shown in Example 1.
  • It is implemented by using deferred execution.

Example 1:




// C# program to print the employees
// names and their gender in the 
// ascending 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 and 
        // gender of the employees in
        // ascending order In query syntax
        var res = from e in emp
                  orderby e.emp_name, e.emp_gender
                  select e;
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee Name: {0} Gender: {1}",
                                 val.emp_name, val.emp_gender);
        }
    }
}


Output:

Employee Name: Anil Gender: Male
Employee Name: Anjita Gender: Female
Employee Name: Anju Gender: Female
Employee Name: Rohit Gender: Male
Employee Name: Soniya Gender: Female
Employee Name: Supriya Gender: Female

Example 2:




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


Output:

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


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