---
title: "What is LINQ sorting operator?"  
description: "What is LINQ sorting operator?"  
author: "Ethan Karla"  
published: 2021-09-24  
updated: 2021-09-24  
canonical: https://www.mindstick.com/forum/156760/what-is-linq-sorting-operator  
category: "linq"  
tags: ["c#", "ado.net", "linq"]  
reading_time: 7 minutes  

---

# What is LINQ sorting operator?

How to use [OrderBy](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance), OrderByDescending, [ThenBy](https://www.mindstick.com/forum/34070/use-thenby-in-linq-query), ThenByDescending, and [Reverse](https://www.mindstick.com/blog/63740/what-are-the-most-effective-and-safest-thanks-to-reverse-erectile-dysfunction) on [sorting](https://www.mindstick.com/articles/1581/sorting-list-with-side-bar) [technique](https://answers.mindstick.com/qa/93922/why-c-sharp-programmers-use-properties-technique-in-c-sharp-programming) in LINQ.

## Replies

### Reply by Ravi Vishwakarma

In LINQ, the sorting operators are used to sort data in ascending or descending order based on one or more attributes in Collection or SQL Server database.

There are following are the different types of sorting operators.

\

| **Operator in LINQ** | **methods in LINQ** | **details of operator** |
| --- | --- | --- |
| orderby | orderby() | sort data by default in ascending order |
| orderby ... descending | OrderByDescending() | sort data by default in descending order |
| orderby ... , ... | ThenBy() | this sort is used for secondary sorting by default ascending order |
| orderby ... , ... descending | ThenByDescending() | this sort is used for secondary sorting by default descending order |
| N/A | Reverse() | this sort the data in reverse order |

## orderby and orderby ... descending

## Syntax

```
datatype variable-name = from object in collection                         where condition                         orderby condition                         select student ;
```

```
datatype variable-name = from object in collection
                         where condition
                         orderby condition descending                         select student ;
```

## Example

```
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
 public static void Main()
 {
  List<Student> students = new List<Student>()
  {
  new Student() { StudentId = 1, Name = 'Asu', Marks = 500 },
  new Student() { StudentId = 2, Name = 'Shyam', Marks = 300 },
  new Student() { StudentId = 3, Name = 'Shriyam', Marks = 400 },
  new Student() { StudentId = 4, Name = 'Sunny', Marks = 550 },
  new Student() { StudentId = 5, Name = 'Ram', Marks = 600 },
  new Student() { StudentId = 6, Name = 'Krishna', Marks = 700 },
  new Student() { StudentId = 7, Name = 'Anupam', Marks = 550 }
  };
  // select projection using first sysntax
  var result1= from student in students
      where student.Marks > 300 && student.Marks < 700
      orderby student.Marks
      select student ;
  Console.WriteLine('Accending order\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  result1.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
  var result2= from student in students
      where student.Marks > 300 && student.Marks < 700
      orderby student.Marks descending
      select student ;
  Console.WriteLine('\nDescending order\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  result2.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
  var result3 = students.Where( student => student.StudentId < 5).OrderBy(stu => stu.Marks);
  Console.WriteLine('\nAccending order using where method Syntax :\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  result3.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
  Console.ReadLine();
 }
}
class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Marks { get; set; }
}
```

## Output:

```
Accending order
Name               ID Marks
Shriyam             3   400
Asu                 1   500
Sunny               4   550
Anupam              7   550
Ram                 5   600
```

```
Descending order
Name               ID Marks
Ram                 5   600
Sunny               4   550
Anupam              7   550
Asu                 1   500
Shriyam             3   400
```

```
Accending order using where method Syntax :
Name               ID Marks
Shyam               2   300
Shriyam             3   400
Asu                 1   500
Sunny               4   550
```

## thenby and theneby... descending

## Syntax

```
datatype variable-name = from object in collection
                         where condition                         orderby condition, condition                         select student ;
```

```
datatype variable-name = from object in collection
                         where condition
                         orderby condition, condition descending
                         select student ;
```

## Example

```
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
 public static void Main()
 {
  List<Student> students = new List<Student>()
  {
  new Student() { StudentId = 1, Name = 'Asu', Marks = 500 },
  new Student() { StudentId = 2, Name = 'Shyam', Marks = 300 },
  new Student() { StudentId = 3, Name = 'Shriyam', Marks = 400 },
  new Student() { StudentId = 4, Name = 'Sunny', Marks = 550 },
  new Student() { StudentId = 5, Name = 'Ram', Marks = 600 },
  new Student() { StudentId = 6, Name = 'Krishna', Marks = 700 },
  new Student() { StudentId = 7, Name = 'Anupam', Marks = 550 }
  };
  // select projection using first sysntax
  var result1= from student in students
      where student.Marks > 300 && student.Marks < 700
      orderby student.Marks, student.StudentId
      select student ;
  Console.WriteLine('Accending order\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  result1.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
  var result2= from student in students
      where student.Marks > 300 && student.Marks < 700
      orderby student.Marks,student.StudentId descending
      select student ;
  Console.WriteLine('\nDescending order\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  result2.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
  var result3 = students.Where( student => student.StudentId < 5).OrderBy(stu => stu.Marks).ThenBy( stu => stu.StudentId);
  Console.WriteLine('\nAccending order using where method Syntax :\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  result3.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
  Console.ReadLine();
 }
}
class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Marks { get; set; }
}
```

## Output

```
Accending order
Name               ID Marks
Shriyam             3   400
Asu                 1   500
Sunny               4   550
Anupam              7   550
Ram                 5   600
```

```
Descending order
Name               ID Marks
Shriyam             3   400
Asu                 1   500
Anupam              7   550
Sunny               4   550
Ram                 5   600
```

```
Accending order using where method Syntax :
Name               ID Marks
Shyam               2   300
Shriyam             3   400
Asu                 1   500
Sunny               4   550
```

## Reverse

```
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
 public static void Main()
 {
  List<Student> students = new List<Student>()
  {
  new Student() { StudentId = 1, Name = 'Asu', Marks = 500 },
  new Student() { StudentId = 2, Name = 'Shyam', Marks = 300 },
  new Student() { StudentId = 3, Name = 'Shriyam', Marks = 400 },
  new Student() { StudentId = 4, Name = 'Sunny', Marks = 550 },
  new Student() { StudentId = 5, Name = 'Ram', Marks = 600 },
  new Student() { StudentId = 6, Name = 'Krishna', Marks = 700 },
  new Student() { StudentId = 7, Name = 'Anupam', Marks = 550 }
  };
  var result3 = students.Where( student => student.StudentId < 5).Reverse();
  Console.WriteLine('\nReverse order using where method Syntax :\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  result3.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
  Console.ReadLine();
 }
}
class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Marks { get; set; }
}
```

## Output

```
Reverse order using where method Syntax :
Name               ID Marks
Sunny               4   550
Shriyam             3   400
Shyam               2   300
Asu                 1   500
```

\


---

Original Source: https://www.mindstick.com/forum/156760/what-is-linq-sorting-operator

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
