8

Does any one know of some kind of Comparator factory in Java, with a

public Comparator getComparatorForClass(Class clazz) {}

It would return Comparators for stuff like String, Double, Integer but would have a

public void addComparatorForClass(Class clazz, Comparator comparator) {}

For arbitrary types.

2
  • 1
    Using Comparable is good option. Such a factory has no sense in general. Commented Oct 26, 2008 at 11:02
  • What would it do for classes that have more than one Comparators? E.g., an Employee that can be sorted by name or by ID? Commented Jan 1, 2011 at 1:02

5 Answers 5

13

Instead of:

factory.getComparatorForClass(x.getClass()).compare(x, y)

you could simply implement Comparable and write:

x.compareTo(y)

String, the primitive wrappers, and standard collections already implement Comparable.

Sign up to request clarification or add additional context in comments.

Comments

10

Use CompareToBuilder from Commons Lang.

Assists in implementing Comparable.compareTo(Object) methods.

To use this class write code as follows:

public class MyClass {
  String field1;
  int field2;
  boolean field3;

  ...

  public int compareTo(Object o) {
    MyClass myClass = (MyClass) o;
    return new CompareToBuilder()
      .appendSuper(super.compareTo(o)
      .append(this.field1, myClass.field1)
      .append(this.field2, myClass.field2)
      .append(this.field3, myClass.field3)
      .toComparison();
  }
}

Comments

4

I'm not aware of anything like that off the top of my head. If you really need something like this, it shouldn't be too difficult to implement one yourself.

However, could you elaborate on why you need something like this? There typically should not be a "default" comparator for classes. If the class has some sort of natural ordering, you really ought to have it implement java.lang.Comparable, and implement Comparable#compareTo(Object) : int instead.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html

Comments

1

Comparator are need to be extend.. this is based on the reusable code implementation.. in these method you can have a custom comparison of data.. the implementation is just simple.. just implement the Comparator interface.. override its compareto method and place the comparison code.. and return which you think is greater in terms of values..

Comments

0

Default comparator is not exist in Java. However, if you are coding with, for example, a customized searcher, which intends to use comparator rather than the compareTo() method of derived type of Comparable class, you may write a static inner comparator class as the default comparator, simply implementing the compare() method by calling the compareTo().

Example:

class Searcher> { private Comparator comparator;

Searcher(Comparator<T> comparator) {
    this.comparator = comparator;
}

Searcher() {
    this(new DefaultComparator<T>());
}

int search(...) {
    ...
}

private static class DefaultComparator<E extends Comparable<E>> 
        implements Comparator<E> {
    public int compare(E o1, E o2) {
        return o1.compareTo(o2);
    }
}

Comments