LINQ Sorting Operators (Order By / Then By / Reverse)

In LINQ, sorting operators are used to change the order or sequence of data (either ascending or descending) based on one or more attributes. The following are the different types of sorting operators available in LINQ.

 

  1. ORDER BY
  2. ORDER BY DESCENDING
  3. THEN BY
  4. THEN BY DESCENDING
  5. REVERSE

These sorting operators are used to sort the data. But the last operator REVERSE reverses the items in the collection. The rest of the operators are used to order the data, just like we use Order by clause in SQL. These sorting operators are similar to what we do in SQL, but only the difference is we are using these operators in LINQ.

 

The following table shows the more detailed information related to sorting operators in LINQ.

 

OperatorDescriptionQuery Syntax
OrderBy This operator will sort values in ascending order. orderby
OrderByDescending This operator will sort values in descending order. orderby .... descending
ThenBy This operator is used to perform secondary sorting in ascending order. orderby ..., ...
ThenByDescending This operator is used to perform sorting in descending order. orderby ..., ... descending
Reverse This operator is used to reverse the order of elements in a collection. Not Applicable.

We will learn all these sorting operators in detail in the coming chapters.