Collections.reverseOrder() in Java with Examples

07 Jul, 2022
The reverseOrder() method of Collections class that in itself is present inside java.util package returns a comparator and using this comparator we can order the Collection in reverse order. Natural ordering is the ordering imposed by the objects’ own compareTo method.
Syntax:
public static  Comparator reverseOrder()
Parameter: A comparator whose ordering is to be reversed by the returned comparator(it can also be null)
Return Type: A comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface. 
Now in order to dig deeper to understand to grassroots, we will be covering different use-cases s listed below as follows:
To sort a list in descending orderTo Sort an Array in Descending OrderTo sort students in descending order of roll numbers when there is a user-defined comparator to do reverse.
To sort a list in descending order
To Sort an Array in Descending Order
To sort students in descending order of roll numbers when there is a user-defined comparator to do reverse.
Case 1: To sort a list in descending order
Example 
Java
// Java Program to Demonstrate Working of reverseOrder()// method of Collections class// To sort a list in descending order  // Importing required utility classesimport java.util.*;  // Main class// Collectionsortingpublic class GFG {      // Main driver method    public static void main(String[] args)    {        // Creating a list of integers for which we        // create an empty ArrayList by        // declaring object of ArrayList class        ArrayList<Integer> al = new ArrayList<Integer>();          // Custom input integer elements        al.add(30);        al.add(20);        al.add(10);        al.add(40);        al.add(50);          // Using sort() method of Collections class to        // sort the elements and passing list and using        // reverseOrder() method to sort in descending order        Collections.sort(al, Collections.reverseOrder());          // Lastly printing the descending sorted list on        // console        System.out.println(            "List after the use of Collection.reverseOrder()"            + " and Collections.sort() :\n" + al);    }}
List after the use of Collection.reverseOrder() and Collections.sort() :
[50, 40, 30, 20, 10]
Note: Geeks now you must be thinking that can we use Arrays.sort()?
Arrays.sort() cannot be used directly to sort primitive arrays in descending order. If we try to call the Arrays.sort() method by passing reverse Comparator defined by Collections.reverseOrder(), it will throw the error as shown below as follows:
Tip: But this will work fine with ‘Array of Objects’ such as the Integer array but will not work with a primitive array such as the int array.
Case 2: To Sort an Array in Descending Order
Example 
Java
// Java Program to Demonstrate Working of reverseOrder()// method of Collections class// To Sort an Array in Descending Order  // Importing required utility classesimport java.util.*;  // Main class// CollectionSortingpublic class GFG {      // Main driver method    public static void main(String[] args)    {          // Creating an array to be sorted in descending        // order        Integer[] arr = { 30, 20, 40, 10 };          // Collections.sort method is sorting the        // elements of arr[] in descending order        // later on Arrays.sort() is applied to sort array        Arrays.sort(arr, Collections.reverseOrder());          // Printing the sorted array on console        System.out.println(            "Array after the use of Collection.reverseOrder()"            + " and Arrays.sort() :\n"            + Arrays.toString(arr));    }}
Array after the use of Collection.reverseOrder() and Arrays.sort() :
[40, 30, 20, 10]
Case 3: To sort students in descending order of roll numbers when there is a user-defined comparator to do reverse.
public static Comparator reverseOrder(Comparator c) 
It returns a Comparator that imposes reverse order of a passed Comparator object. We can use this method to sort a list in reverse order of user-defined Comparator. For example, in the below program, we have created a reverse of the user-defined comparator to sort students in descending order of roll numbers. 
Example:
Java
// Java Program to Demonstrate Working of// reverseOrder(Comparator c)// To sort students in descending order of roll numbers// when there is a user defined comparator to do reverse  // Importing required classesimport java.io.*;import java.lang.*;import java.util.*;  // Class 1// Helper student class// to represent a studentclass Student {    int rollno;    String name, address;      // Constructor    public Student(int rollno, String name, String address)    {          // This keyword refers to current instance itself        this.rollno = rollno;        this.name = name;        this.address = address;    }      // Method of Student class    // To print student details inside main() method    public String toString()    {          return this.rollno + " " + this.name + " "            + this.address;    }}  // Class 2// Helper class implementing interfaceclass Sortbyroll implements Comparator<Student> {      // Method    // Used for sorting in ascending order of    // roll number    public int compare(Student a, Student b)    {        return a.rollno - b.rollno;    }}  // Class 3// Main classclass GFG {      // Main driver method    public static void main(String[] args)    {          // Creating an empty ArrayList        ArrayList<Student> ar = new ArrayList<Student>();          // Adding custom attributes defined in Student class        // using add() method        ar.add(new Student(111, "bbbb", "london"));        ar.add(new Student(131, "aaaa", "nyc"));        ar.add(new Student(121, "cccc", "jaipur"));          // Display message for better readability        System.out.println("Unsorted");          // Printing list of students        for (int i = 0; i < ar.size(); i++)            System.out.println(ar.get(i));          // Sorting a list of students in descending order of        // roll numbers using a Comparator        // that is reverse of Sortbyroll()        Comparator c            = Collections.reverseOrder(new Sortbyroll());        Collections.sort(ar, c);          // Display message for better readability        System.out.println("\nSorted by rollno");          // Printing sorted students in descending order        for (int i = 0; i < ar.size(); i++)            System.out.println(ar.get(i));    }}
Output: 
Unsorted
111 bbbb london
131 aaaa nyc
121 cccc jaipur

Sorted by rollno
131 aaaa nyc
121 cccc jaipur
111 bbbb london
The key thing here to remember is above program uses unchecked and unsafe operations.
This article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.  
prasanthibonam259
solankimayank
kashishsoda
simmytarika5
germanshephered48
Java - util package
Java-Collections
Java-Collections-Class
Java-Functions
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Collections in Java
Stream In Java
Multidimensional Arrays in Java
Stack Class in Java
Singleton Class in Java
Set in Java
Introduction to Java