LINQ | Sorting Operator | Reverse

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:

  1. OrderBy
  2. OrderByDescending
  3. ThenBy
  4. ThenByDescending
  5. Reverse

Reverse Operator

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to reverse the order
// of the given array
using 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);
        }
    }
}

chevron_right


Output:

The Sequence is: 
1
2
3
4
5
6
7
New Sequence:
7
6
5
4
3
2
1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print the 
// name of the employees
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 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);
        }
    }
}

chevron_right


Output:

Employee Name: Supriya
Employee Name: Soniya
Employee Name: Rohit
Employee Name: Anju
Employee Name: Anjita
Employee Name: Anil


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.