Created
July 3, 2018 16:06
-
-
Save abdalin/0099fd97f2265f9ac2c1a0ce73ad629c to your computer and use it in GitHub Desktop.
java special aggregate of multiple comparators
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
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