LINQ | Sorting Operator | Reverse - GeeksforGeeks

22 May, 2019
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:
OrderByOrderByDescendingThenByThenByDescendingReverse
OrderBy
OrderByDescending
ThenBy
ThenByDescending
Reverse
The reverse operator is used to reverse the order of the sequence or collection. Unlike the OrderBy Method, it does not consider the actual value in itself in determining the order, instead of this, it returns the items in the reverse order in which they actually present.For example, the collection is present in ascending order when you use the Reverse Method on the given collection it reverses the order of the collection, i.e, in descending order.
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.
It is implemented by using deferred execution.
Example 1:
// C# program to reverse the order// of the given arrayusing System;using System.Linq;  class GFG {      // Main Method    static public void Main()    {          // Data source        int[] sequence = {1, 2, 3, 4, 5, 6, 7};          // Display the sequence        Console.WriteLine("The Sequence is: ");          foreach(int s in sequence)        {            Console.WriteLine(s);        }          // Reverse the given array        // Using Reverse function        var result = sequence.Reverse();          Console.WriteLine("New Sequence:");        foreach(var val in result)        {            Console.WriteLine(val);        }    }}
The Sequence is: 
1
2
3
4
5
6
7
New Sequence:
7
6
5
4
3
2
1

Example 2:
// C# program to print the // name of the employeesusing System;using System.Linq;using System.Collections.Generic;  // Employee detailspublic 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 employees using        // Reverse method        var res = emp.OrderBy(e => e.emp_name).Reverse();          foreach(var val in res)        {            Console.WriteLine("Employee Name: {0}", val.emp_name);        }    }}
Employee Name: Supriya
Employee Name: Soniya
Employee Name: Rohit
Employee Name: Anju
Employee Name: Anjita
Employee Name: Anil

CSharp LINQ
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
C# | Inheritance
Partial Classes in C#
C# | Generics - Introduction
Top 50 C# Interview Questions & Answers
Switch Statement in C#
C# | How to insert an element in an Array?
Convert String to Character Array in C#
Linked List Implementation in C#