The most straightforward way to do this is to simply have the enum itself implement Comparator:
public enum SortBy implements Comparator<Item> {
CATEGORY {
@Override
public final int compare(final Item o1, final Item o2) {
return compareStrings(o1.getCategory(), o2.getCategory());
}
},
ENGLISH {
@Override
public final int compare(final Item o1, final Item o2) {
return compareStrings(o1.getEnglish(), o2.getEnglish());
}
},
NORWEGIAN {
@Override
public final int compare(final Item o1, final Item o2) {
return compareStrings(o1.getNorwegian(), o2.getNorwegian());
}
};
private static int compareStrings(final String s1, final String s2) {
if (s1 == null) {
return s2 == null ? 0 : -1;
}
if (s2 == null) {
return 1;
}
return s1.compareTo(s2);
}
}
There's no need to further associate each enum member with a comparator because the enum members are the comparators. The usage is then nice and concise:
Collections.sort(items, SortBy.CATEGORY);
Addendum
In the comments below, you asked about common, null-safe comparisons. I'm not sure what third-party libraries might provide them, but you can implement them yourself easily enough. Here's two from one of our internal libraries:
static <T extends Comparable<? super T>> int compare(final T o1, final T o2) {
if (o1 == null) {
return o2 == null ? 0 : -1;
}
if (o2 == null) {
return 1;
}
return o1.compareTo(o2);
}
@SuppressWarnings("unchecked")
static int compare(final Object a, final Object b) {
if (a == b) {
return 0;
}
if (a == null) {
return -1;
}
if (b == null) {
return 1;
}
final Class<?> aClass = a.getClass();
final Class<?> bClass = b.getClass();
if (Comparable.class.isInstance(a) && aClass.isAssignableFrom(bClass)) {
return ((Comparable<Object>)a).compareTo(b);
}
if (Comparable.class.isInstance(b) && bClass.isAssignableFrom(aClass)) {
return ((Comparable<Object>)b).compareTo(a);
}
throw new IllegalArgumentException("Values must be comparable.");
}