Skip to content

Instantly share code, notes, and snippets.

@abdalin
Created July 3, 2018 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdalin/0099fd97f2265f9ac2c1a0ce73ad629c to your computer and use it in GitHub Desktop.
Save abdalin/0099fd97f2265f9ac2c1a0ce73ad629c to your computer and use it in GitHub Desktop.
java special aggregate of multiple comparators
class MultiComparator<T> implements Comparator<T> {
private final List<Comparator<T>> comparators;
public MultiComparator(List<Comparator<T>> comparators) {
this.comparators = comparators;
}
public MultiComparator(Comparator<T>... comparators) {
this(Arrays.asList(comparators));
}
public int compare(T o1, T o2) {
for (Comparator<T> c : comparators) {
int result = c.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