Collections.reverseOrder() in Java with Examples
Last Updated :
10 Jan, 2023
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 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 classes
import java.util.*;
// Main class
// Collectionsorting
public 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);
}
}
OutputList 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 classes
import java.util.*;
// Main class
// CollectionSorting
public 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));
}
}
OutputArray 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 classes
import java.io.*;
import java.lang.*;
import java.util.*;
// Class 1
// Helper student class
// to represent a student
class 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 interface
class 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 class
class 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.
Similar Reads
Collections.sort() in Java with Examples
java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order. It works similar to java.util.Arrays.sort() method but it is better than as it can sort the elements of Array as well as link
5 min read
Collections.reverse() Method in Java with Examples
The reverse() method of the Collections class, as the name suggests, is used to reverse the order of elements in a list. Note: It does not sort the elements, it simply reverses their current order.This class is present in java.util package so the syntax is as follows:import java.util.Collections;Col
3 min read
Collections list() method in Java with Examples
The list() method of java.util.Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that requi
2 min read
Collections min() method in Java with Examples
min(Collection<? extends T> coll) The min() method of java.util.Collections class is used to return the minimum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements
4 min read
Collections max() method in Java with Examples
max(Collection<? extends T> coll) The max() method of java.util.Collections class is used to return the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements
5 min read
Collections swap() method in Java with Examples
The swap() method of java.util.Collections class is used to swap the elements at the specified positions in the specified list. If the specified positions are equal, invoking this method leaves the list unchanged. Syntax: public static void swap(List list, int i, int j) Parameters: This method takes
2 min read
Collections copy() method in Java with Examples
The copy() method of java.util.Collections class is used to copy all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the sourc
3 min read
Collectors toSet() in Java with Examples
Collectors toSet() returns a Collector that accumulates the input elements into a new Set. There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned. This is an unordered Collector i.e, the collection operation does not commit to preserving the encounter
2 min read
Collection add() Method in Java with Examples
The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet
4 min read
Comparator reverseOrder() method in Java with examples
The reverseOrder() method of Comparator Interface in Java returns a comparator that use to compare Comparable objects in reverse of natural order. The returned comparator by this method is serializable and throws NullPointerException when comparing null. Syntax: static <T extends Comparable<T
1 min read