Collections.reverseOrder() in Java with Examples
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayList<Integer> al = new ArrayList<Integer>();
al.add( 30 );
al.add( 20 );
al.add( 10 );
al.add( 40 );
al.add( 50 );
Collections.sort(al, Collections.reverseOrder());
System.out.println(
"List after the use of Collection.reverseOrder()"
+ " and Collections.sort() :\n" + al);
}
}
|
Output
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
Integer[] arr = { 30 , 20 , 40 , 10 };
Arrays.sort(arr, Collections.reverseOrder());
System.out.println(
"Array after the use of Collection.reverseOrder()"
+ " and Arrays.sort() :\n"
+ Arrays.toString(arr));
}
}
|
Output
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
import java.io.*;
import java.lang.*;
import java.util.*;
class Student {
int rollno;
String name, address;
public Student( int rollno, String name, String address)
{
this .rollno = rollno;
this .name = name;
this .address = address;
}
public String toString()
{
return this .rollno + " " + this .name + " "
+ this .address;
}
}
class Sortbyroll implements Comparator<Student> {
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
class GFG {
public static void main(String[] args)
{
ArrayList<Student> ar = new ArrayList<Student>();
ar.add( new Student( 111 , "bbbb" , "london" ));
ar.add( new Student( 131 , "aaaa" , "nyc" ));
ar.add( new Student( 121 , "cccc" , "jaipur" ));
System.out.println( "Unsorted" );
for ( int i = 0 ; i < ar.size(); i++)
System.out.println(ar.get(i));
Comparator c
= Collections.reverseOrder( new Sortbyroll());
Collections.sort(ar, c);
System.out.println( "\nSorted by rollno" );
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.
Last Updated :
10 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...