;

# Sorting Operators: OrderBy & OrderByDescending

In this tutorial, we will learn about sorting operators - OrderBy & OrderByDescending in LINQ

## Sorting Operator

In LINQ, sorting operators are used to sort the data from the collection in a specific order such as in ascending or descending order. In this tutorial, we will learn about the sorting operators - OrderBy & OrderByDescending.

## OrderBy

In LINQ, `OrderBy` is used to sort the data in the collection based on a specific field in a specific order i.e. either in ascending order or in descending order.

By default, it sorts the data in the collection in an ascending order.

The `ascending` keyword is optional but if you want to sort the collection in descending order, use the `descending` keyword.

## Example 1: OrderBy Operator using Query Syntax

In this example, we used the `OrderBy `operator and sorted the collection in ascending and descending order. By default, `OrderBy` sorts the collection in ascending order, we use the `descending` keyword for sorting the collection in descending order.

##### Example 1: OrderBy Operator using Query Syntax
``````using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
public static void Main()
{
// Employee collection
IList<Employee> employeelist = new List<Employee>() {
new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 }
};

// LINQ Query Syntax to sort an Employe
// in an Ascending and Descending Order based on EmpName
Console.WriteLine("--Employee Data in Ascending Order--");
var orderByResult = from s in employeelist
orderby s.EmpName
select s;
foreach (var emp in orderByResult)
{
Console.WriteLine(\$"EmpId = {emp.EmpID}; EmpName = {emp.EmpName}; City = {emp.City}");
}
Console.WriteLine();
Console.WriteLine("--Employee Data in Descending Order--");
var orderByDescendingResult = from s in employeelist
orderby s.EmpName descending
select s;
foreach (var emp in orderByDescendingResult)
{
Console.WriteLine(\$"EmpId = {emp.EmpID}; EmpName = {emp.EmpName}; City = {emp.City}");
}

}

}

public class Employee
{

public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
public int Salary { get; set; }

}
``````
##### Output
`--Employee Data in Ascending Order--EmpId = 3; EmpName = Bill; City = SeattleEmpId = 1; EmpName = John; City = New YorkEmpId = 2; EmpName = Moin; City = New OrleansEmpId = 4; EmpName = Ram; City = DelhiEmpId = 5; EmpName = Ron; City = San Jose--Employee Data in Descending Order--EmpId = 5; EmpName = Ron; City = San JoseEmpId = 4; EmpName = Ram; City = DelhiEmpId = 2; EmpName = Moin; City = New OrleansEmpId = 1; EmpName = John; City = New YorkEmpId = 3; EmpName = Bill; City = Seattle`

## Example 2: OrderBy Operator using Method Syntax

When you use `OrderBy` with the method syntax, then this `OrderBy` extension method has two overloads:

The first overload method of `OrderBy` accepts the Func delegate type parameter, so you just need to pass the lambda expression for the field-based sorting on the collection.

``````public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector);
``````

The second overload of `OrderBy` accepts the object of `IComparer` along with the Func delegate type parameter to use custom comparison for sorting collection.

``````public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer);
``````

Here is the example of OrderBy using Method Syntax:

##### Example 2: OrderBy Operator using Method Syntax
``````using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqPrograms
{
internal class Program
{
public static void Main(string[] args)
{
// Employee collection
IList<Employee> employeelist = new List<Employee>() {
new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 }
};

// LINQ Method Syntax to sort an Employe
// in an Ascending Order based on EmpName
Console.WriteLine("--Employee Data in Ascending Order--");
var orderByResult = employeelist.OrderBy(e => e.EmpName);

foreach (var emp in orderByResult)
{
Console.WriteLine(\$"EmpId = {emp.EmpID}; EmpName = {emp.EmpName}; City = {emp.City}");
}

}

}

public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
public int Salary { get; set; }
}

}
``````
##### Output
`--Employee Data in Ascending Order--EmpId = 3; EmpName = Bill; City = SeattleEmpId = 1; EmpName = John; City = New YorkEmpId = 2; EmpName = Moin; City = New OrleansEmpId = 4; EmpName = Ram; City = DelhiEmpId = 5; EmpName = Ron; City = San Jose`

## OrderByDescending

In LINQ, `OrderByDescending()` is used to sort the data in the collection based on specific fields in descending order.  This method supports only method syntax. Here is an example to sort the collection using `OrderByDescending()` method:

##### Example: OrderByDescending
``````using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqPrograms
{
internal class Program
{
public static void Main(string[] args)
{
// Employee collection
IList<Employee> employeelist = new List<Employee>() {
new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 }
};

// LINQ Method Syntax to sort an Employe
// in a Descending Order based on EmpName
Console.WriteLine("--Employee Data in Descending Order--");
var orderByResult = employeelist.OrderByDescending(e => e.EmpName);

foreach (var emp in orderByResult)
{
Console.WriteLine(\$"EmpId = {emp.EmpID}; EmpName = {emp.EmpName}; City = {emp.City}");
}

}

}

public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
public int Salary { get; set; }
}

}
``````
##### Output
`--Employee Data in Descending Order--EmpId = 5; EmpName = Ron; City = San JoseEmpId = 4; EmpName = Ram; City = DelhiEmpId = 2; EmpName = Moin; City = New OrleansEmpId = 1; EmpName = John; City = New YorkEmpId = 3; EmpName = Bill; City = Seattle`

## Sort using Multiple Fields

In LINQ, you can sort the collection on multiple fields which are separated by a comma. Here is an example to sort the collection using multiple fields:

##### Example: Sorting the collection using multiple fields
``````using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqPrograms
{
internal class Program
{
public static void Main(string[] args)
{
// Employee collection
IList<Employee> employeelist = new List<Employee>() {
new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 },
new Employee() { EmpID = 6, EmpName = "Bill" , City = "Chicago", Salary = 250000 }
};

// LINQ Query Syntax to sort an Employe
// in a ascending Order based on multiple
// fields like EmpName, Salary
Console.WriteLine("--Employee Data Sorted on Multiple Field in Ascending Order--");
var orderByResult = from s in employeelist
orderby s.EmpName, s.Salary
select s;

foreach (var emp in orderByResult)
{
Console.WriteLine(\$"EmpId = {emp.EmpID}; EmpName = {emp.EmpName}; City = {emp.City}; Salary = {emp.Salary}");
}

}

}

public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
public int Salary { get; set; }
}

}
``````
##### Output
`--Employee Data Sorted on Multiple Field in Ascending Order--EmpId = 3; EmpName = Bill; City = Seattle; Salary = 180000EmpId = 6; EmpName = Bill; City = Chicago; Salary = 250000EmpId = 1; EmpName = John; City = New York; Salary = 130000EmpId = 2; EmpName = Moin; City = New Orleans; Salary = 210000EmpId = 4; EmpName = Ram; City = Delhi; Salary = 200000EmpId = 5; EmpName = Ron; City = San Jose; Salary = 150000`

Learn Sorting Operator - ThenBy & ThenByDescending in the next tutorial.