Skip to content

Instantly share code, notes, and snippets.

@josephmilla
Created September 29, 2014 00:04
Show Gist options
  • Save josephmilla/e456e4422a6ef731ecfc to your computer and use it in GitHub Desktop.
Save josephmilla/e456e4422a6ef731ecfc to your computer and use it in GitHub Desktop.
enum PersonComparator implements Comparator<Person> {
ID_SORT {
public int compare(Person o1, Person o2) {
return Integer.valueOf(o1.getId()).compareTo(o2.getId());
}},
NAME_SORT {
public int compare(Person o1, Person o2) {
return o1.getFullName().compareTo(o2.getFullName());
}};
public static Comparator<Person> decending(final Comparator<Person> other) {
return new Comparator<Person>() {
public int compare(Person o1, Person o2) {
return -1 * other.compare(o1, o2);
}
};
}
public static Comparator<Person> getComparator(final PersonComparator... multipleOptions) {
return new Comparator<Person>() {
public int compare(Person o1, Person o2) {
for (PersonComparator option : multipleOptions) {
int result = option.compare(o1, o2);
if (result != 0) {
return result;
}
}
return 0;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment