java.util.Collections.reverseOrder() Method
Advertisements
Description
The reverseOrder() method is used to get a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.
Declaration
Following is the declaration for java.util.Collections.reverseOrder() method.
public static <T> Comparator<T> reverseOrder()
Parameters
NA
Return Value
The method call returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.
Exception
NA
Example
The following example shows the usage of java.util.Collections.reverseOrder()
package com.tutorialspoint;
import java.util.*;
public class CollectionsDemo {
public static void main(String args[]) {
// create linked list object
LinkedList list = new LinkedList();
// populate the list
list.add(-28);
list.add(20);
list.add(-12);
list.add(8);
// create comparator for reverse order
Comparator cmp = Collections.reverseOrder();
// sort the list
Collections.sort(list, cmp);
System.out.println("List sorted in ReverseOrder: ");
for(int i : list){
System.out.println(i+ " ");
}
}
}
Let us compile and run the above program, this will produce the following result.
List sorted in ReverseOrder: 20 8 -12 -28