Created
April 4, 2011 15:07
-
-
Save gamlerhart/901785 to your computer and use it in GitHub Desktop.
CompositeComparator.java
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
public final class CompositeComparator<T> implements Comparator<T>{ | |
private final List<Comparator<? super T>> comparators; | |
CompositeComparator(List<Comparator<? super T>> comparators) { | |
this.comparators = comparators; | |
} | |
public static <TCompare> Comparator<TCompare> compareWith(Comparator<? super TCompare>... comparators){ | |
return new CompositeComparator<TCompare>(Arrays.asList(comparators)); | |
} | |
public int compare(T o1, T o2) { | |
for (Comparator<? super T> comparator : comparators) { | |
int result = comparator.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