Generic comparator to sort Java Collections (List, Set)

28,443

Solution 1

The Collections class has a reverseOrder method which returns a comparator for a generic type T which should satisfy your requirement for the DESC comparator. If you are passing your Collection to the Collections.sort() method, it automatically uses the ASC sort.

Also, "sorting" doesn't mean a lot when it comes to "sets" which maintain "unique" elements in an unordered fashion (I mean you can use TreeSet for sorted sets, but that's a different story). A simple workaround would be to make a List out of the Set and pass it to Collections.sort.

Solution 2

Using GenericComparator.java you will be able to sort following datatypes Integer, String, Long, Double, Float, and Date.

For Ascending order

Collections.sort(persons, new GenericComparator("name", true));

For Descending order

Collections.sort(persons, new GenericComparator("name", false));

Detailed information here!

Solution 3

This might work as a basic kick-off example to start with your requirement :

// Create a list
String[] strArray = new String[] {"z", "a", "C"};
List list = Arrays.asList(strArray);

// Sort
Collections.sort(list);
// C, a, z

// Case-insensitive sort
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
// a, C, z

// Reverse-order sort
Collections.sort(list, Collections.reverseOrder());
// z, a, C

// Case-insensitive reverse-order sort
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(list);
// z, C, a

Solution 4

You have to call your comparator by using following code.

Collection.sort(myList,new MyComparatorClass());

Then you have to create your own comparator class which will perform sorting on your object bean either on ascending or descending order.In that class you can compare the property on which you want to perform sort.Here I perform sorting on the name.

public class MyComparator  implements Comparator {

    public int compare(Object o1, Object o2) {
      return o1.getName().compareTo(o2.getName());
    }

}

Solution 5

Define a Comparator interface like

public interface Comparator<T> {
  int compare(T o1, T o2);
  boolean equals(Object obj);
}

Write classes which are identified to be compared for example for date objects

public class DateOrderComparator implements Comparator

for Rank Comparator

public class RankCodeComparator implements Comparator

and so on...

Share:
28,443
Julia
Author by

Julia

Updated on December 18, 2020

Comments

  • Julia
    Julia over 3 years

    I need some generic comparator which would accept instances of List or Set as an argument, and order direction argument (ASC, DESC), and then return the sorted collection. I can not seem to find on internet this example, and am in a horrible rush. I know I dont ask question in appropriate way, since I dont have any code to begin with but am in a horrible rush. Collections will contain objects which implement comparable and dates.

    Any examples, implementations very much appreciated. Thank you.

  • jeevatkm
    jeevatkm about 9 years
    Please let me know your reason for down vote. So that I can provide an explanation or improve my answer, Thanks.