Created
September 29, 2014 00:04
-
-
Save josephmilla/e456e4422a6ef731ecfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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