LINQ | Sorting Operator | OrderBy
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:
- OrderBy
- OrderByDescending
- ThenBy
- ThenByDescending
- Reverse
OrderBy Operator
OrderBy operator is used to rearranging the elements of the given sequence in ascending order. This operator by default converts the order of the given sequence in ascending order. There is no need to add an extra ascending condition in the query expression means ascending keyword is optional. You can also use the descending keyword to change the order of the given sequence in descending order.
OrderBy in Query Syntax: OrderBy operator support query syntax in both C# and VB.Net language. In query syntax, the orderby word is used as shown in the below example:
Example:
// C# program to print the employee // name in 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 ; } } public 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 // employee in ascending order // Using orderby clause in Query Syntax var res = from e in emp orderby e.emp_name select e.emp_name; foreach ( var val in res) { Console.WriteLine( "Employee Name: {0}" , val); } } } |
Output:
Employee Name: Anil Employee Name: Anjita Employee Name: Anju Employee Name: Rohit Employee Name: Soniya Employee Name: Supriya
OrderBy in Method Syntax: OrderBy operator in method syntax is overloaded in two different types:
- OrderBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>): This method sort the items of the given sequence in ascending order according to the key.
- OrderBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>): This method sort the items of the given sequence in ascending order by using a specified comparer.
It present in both the Queryable and Enumerable class. And support method syntax in both C# and VB.NET languages. As shown in the below example:
Example:
// C# program to print the salary of // the employees in ascending 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 ; } } public 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 = 80000}, new Employee() {emp_id = 213, emp_name = "Anil" , emp_gender = "Male" , emp_hire_date = "12/1/2016" , emp_salary = 60000}, new Employee() {emp_id = 214, emp_name = "Anju" , emp_gender = "Female" , emp_hire_date = "17/6/2015" , emp_salary = 50000}, }; // Print the salary of the employees // in ascending order using the // OrderBy operator var res = emp.OrderBy(a => a.emp_salary); Console.WriteLine( "Salary of the employees: " ); foreach ( var val in res) { Console.WriteLine(val.emp_salary); } } } |
Output:
Salary of the employees: 20000 30000 40000 50000 60000 80000
Multiple Sorting
In LINQ, you are allowed to sort the multiple fields of the collection in the single query and each field is separated by a comma. When you perform multiple sorting the collection first sort according to the first condition and then if the two fields in the given collection are similar then it would sort the second field and so on. As shown in the below example, emp_salary contain two similar values so the sorting is done according to emp_id.
Example:
// C# program to illustrate multiple sorting 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 ; } } public 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 = 20000}, 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 = 80000}, new Employee() {emp_id = 213, emp_name = "Anil" , emp_gender = "Male" , emp_hire_date = "12/1/2016" , emp_salary = 60000}, new Employee() {emp_id = 214, emp_name = "Anju" , emp_gender = "Female" , emp_hire_date = "17/6/2015" , emp_salary = 50000}, }; // Perform multiple sorting var res = from e in emp orderby e.emp_salary, e.emp_id select e; foreach ( var val in res) { Console.WriteLine( "Employee Salary: {0} Employee Id: {1}" , val.emp_salary, val.emp_id); } } } |
Output:
Employee Salary: 20000 Employee Id: 209 Employee Salary: 20000 Employee Id: 210 Employee Salary: 40000 Employee Id: 211 Employee Salary: 50000 Employee Id: 214 Employee Salary: 60000 Employee Id: 213 Employee Salary: 80000 Employee Id: 212
Please Login to comment...