Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public interface Comparator<T>
Known subtypesCaseInsensitiveOrder
Comparator is used to compare two objects to determine their ordering with
respect to each other. On a given Collection, a Comparator can be used to
obtain a sorted Collection which is totally ordered. For a Comparator
to be consistent with equals, its {code #compare(Object, Object)}
method has to return zero for each pair of elements (a,b) where a.equals(b)
holds true. It is recommended that a Comparator implements
java.io.Serializable.Methods
Method details
compare
public abstract int compare(T object1, T object2)Compares the two specified objects to determine their relative ordering. The ordering
implied by the return value of this method for all possible pairs of
(object1, object2) should form an equivalence relation.
This means that
compare(a,a)returns zero for allathe sign of
compare(a,b)must be the opposite of the sign ofcompare(b,a)for all pairs of (a,b)From
compare(a,b) > 0andcompare(b,c) > 0it must followcompare(a,c) > 0for all possible combinations of(a,b,c)
Parameters
object1T- an
Object. object2T- a second
Objectto compare withobject1.
Returns
object1 is greater than object2.Throws
ClassCastException- if objects are not of the correct type.
equals
public abstract boolean equals(Object object)Compares this Comparator with the specified Object and indicates whether they
are equal. In order to be equal, object must represent the same object
as this instance using a class-specific comparison.
A Comparator never needs to override this method, but may choose so for
performance reasons.
Parameters
objectObject- the
Objectto compare with this comparator.
Returns
true if specified Object is the same as this
Object, and false otherwise.See also
reversed
public default Comparator<T> reversed()Returns
thenComparing
public default Comparator<T> thenComparing(Comparator<? super T> other)other.Parameters
otherComparator<? super T>- the tie breaker
Returns
thenComparing
public default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor)Parameters
keyExtractorFunction<? super T, ? extends U>- extracts the key
<U>- the key type
Returns
comparing
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor)Parameters
keyExtractorFunction<? super T, ? extends U>- extracts the key
<T>- the compared type
<U>- the key type
Returns
comparing
public static <T, U> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator)Parameters
keyExtractorFunction<? super T, ? extends U>- extracts the key
keyComparatorComparator<? super U>- orders the keys
<T>- the compared type
<U>- the key type
Returns
naturalOrder
public static <T extends Comparable<? super T>> Comparator<T> naturalOrder()Parameters
<T>- the compared type
Returns
reverseOrder
public static <T extends Comparable<? super T>> Comparator<T> reverseOrder()Parameters
<T>- the compared type
Returns
thenComparing
public default <U> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator)keyComparator gives.Parameters
keyExtractorFunction<? super T, ? extends U>- extracts the key
keyComparatorComparator<? super U>- orders the keys
<U>- the key type
Returns
thenComparingInt
public default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor)int key.Parameters
keyExtractorToIntFunction<? super T>- extracts the key
Returns
thenComparingLong
public default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor)long key.Parameters
keyExtractorToLongFunction<? super T>- extracts the key
Returns
thenComparingDouble
public default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)double key, compared as Double.compare does.Parameters
keyExtractorToDoubleFunction<? super T>- extracts the key
Returns
comparingInt
public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)int key.Parameters
keyExtractorToIntFunction<? super T>- extracts the key
<T>- the compared type
Returns
comparingLong
public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor)long key.Parameters
keyExtractorToLongFunction<? super T>- extracts the key
<T>- the compared type
Returns
comparingDouble
public static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor)Parameters
keyExtractorToDoubleFunction<? super T>- extracts the key
<T>- the compared type
Returns
nullsFirst
public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)comparator;
with a null comparator the others are all equal.Parameters
comparatorComparator<? super T>- orders the values that are not null, or null
<T>- the compared type
Returns
nullsLast
public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)comparator;
with a null comparator the others are all equal.Parameters
comparatorComparator<? super T>- orders the values that are not null, or null
<T>- the compared type