java.util.Collections.reverseOrder() Method


Advertisements


Description

The reverseOrder(Comparator<T>) method is used to get a comparator that imposes the reverse ordering of the specified comparator.

Declaration

Following is the declaration for java.util.Collections.reverseOrder() method.

public static <T> Comparator<T> reverseOrder(Comparator<T> cmp)

Parameters

cmp − This is the comparator.

Return Value

The method call returns a comparator that imposes the reverse ordering of the specified comparator.

Exception

NA

Example

The following example shows the usage of java.util.Collections.reverseOrder()

Live Demo
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {  
      
      // create linked list object  	   
      LinkedList<Integer> list = new LinkedList<Integer>();  

      // populate the list 
      list.add(-28);  
      list.add(20);  
      list.add(-12);  
      list.add(8);  

      // create comparator for reverse order
      Comparator<Integer> cmp = Collections.reverseOrder(null);  

      // 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 

java_util_collections.htm

Advertisements
E-Books Store