Returning a comparator that imposes the reverse ordering of the specified comparator

Returning 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).

package collection.demos;



import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;



class Comparator1 implements Comparator<String>{

    public int compare(String o1, String o2) {

        return o1.compareTo(o2);

    }

}

public class GettingReverseOrderCamparator2 {

    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(new Comparator1());

        //Applying comparator to list

        Collections.sort(list, c);

        System.out.println("List : "+list);

    }

}

/*OUTPUT

 * List : [D, C, B, A]

 */

No comments:

Popular Posts