Returning a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface. (The natural ordering is the ordering imposed by the objects' own compareTo method.) This enables a simple idiom for sorting (or maintaining) collections (or arrays) of objects that implement the Comparable interface in reverse-natural-order.
package collection.demos;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class GettingReverseOrderCamparator {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
Comparator c=Collections.reverseOrder();
//Applying comparator to list
Collections.sort(list, c);
System.out.println("List : "+list);
}
}
/*OUTPUT
* List : [D, C, B, A]
*/
package collection.demos;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class GettingReverseOrderCamparator {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
Comparator c=Collections.reverseOrder();
//Applying comparator to list
Collections.sort(list, c);
System.out.println("List : "+list);
}
}
/*OUTPUT
* List : [D, C, B, A]
*/
Comments