Collections public static Comparator reverseOrder(Comparator cmp) Method Example Program


Returns a comparator that imposes the reverse ordering of the specified comparator.

Program

import java.util.*;

/**
 * 
 * @author karthikeyan.T
 * @description : the following code shows how to use
 *              Collections.reverseOrder(Comparator<T> cmp) method.
 */
public class CollectionsReverseOrder {

	public static void main(String args[]) {
		LinkedList<Integer> list = new LinkedList();
		list.add(55);
		list.add(65);
		list.add(32);
		list.add(99);
		Comparator cmp = Collections.reverseOrder();
		Collections.sort(list, cmp);
		System.out.println("List sorted in ReverseOrder: ");
		for (int i : list) {
			System.out.println(i + " ");
		}
	}
}

Output

List sorted in ReverseOrder: 
99 
65 
55 
32 

Explanation

public static Comparator reverseOrder(Comparator cmp)
Returns a comparator that imposes the reverse ordering of the specified comparator. If the specified comparator is null, this method is equivalent to reverseOrder() (in other words, it returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface).
The returned comparator is serializable (assuming the specified comparator is also serializable or null).

Parameters:
cmp - a comparator who's ordering is to be reversed by the returned comparator or null
Returns:
A comparator that imposes the reverse ordering of the specified comparator.
Since:
1.5


Related Post

Comments


©candidjava.com