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

A 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

public abstract int compare(T object1, T object2)Compares the two specified objects to determine their relative ordering.
public abstract boolean equals(Object object)Compares this Comparator with the specified Object and indicates whether they are equal.
public default Comparator<T> reversed()This ordering, reversed.
public default Comparator<T> thenComparing(Comparator<? super T> other)This ordering, ties broken by other.
public default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor)This ordering, ties broken by the natural order of a key.
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor)Compares by the natural order of a key.
public static <T, U> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator)Compares by a key, using the given comparator for the keys.
public static <T extends Comparable<? super T>> Comparator<T> naturalOrder()The natural order of comparable values.
public static <T extends Comparable<? super T>> Comparator<T> reverseOrder()The reverse of the natural order.
public default <U> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator)This ordering, ties broken by a key in the order keyComparator gives.
public default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor)This ordering, ties broken by an int key.
public default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor)This ordering, ties broken by a long key.
public default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)This ordering, ties broken by a double key, compared as Double.compare does.
public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)Compares by an int key.
public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor)Compares by a long key.
public static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor)Compares by a double key, as Double.compare does: NaN above everything and -0.0 below 0.0.
public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)Orders null before every other value, and the rest by comparator; with a null comparator the others are all equal.
public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)Orders null after every other value, and the rest by comparator; with a null comparator the others are all equal.

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 all a

  • the sign of compare(a,b) must be the opposite of the sign of compare(b,a) for all pairs of (a,b)

  • From compare(a,b) > 0 and compare(b,c) > 0 it must follow compare(a,c) > 0 for all possible combinations of (a,b,c)

Parameters

object1 T
an Object.
object2 T
a second Object to compare with object1.

Returns

an integer 0 if 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

object Object
the Object to compare with this comparator.

Returns

boolean true if specified Object is the same as this Object, and false otherwise.

reversed

public default Comparator<T> reversed()
This ordering, reversed.

Returns

the reversed comparator

thenComparing

public default Comparator<T> thenComparing(Comparator<? super T> other)
This ordering, ties broken by other.

Parameters

other Comparator<? super T>
the tie breaker

Returns

the combined comparator

thenComparing

public default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor)
This ordering, ties broken by the natural order of a key.

Parameters

keyExtractor Function<? super T, ? extends U>
extracts the key
<U>
the key type

Returns

the combined comparator

comparing

public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor)
Compares by the natural order of a key.

Parameters

keyExtractor Function<? super T, ? extends U>
extracts the key
<T>
the compared type
<U>
the key type

Returns

the comparator

comparing

public static <T, U> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator)
Compares by a key, using the given comparator for the keys.

Parameters

keyExtractor Function<? super T, ? extends U>
extracts the key
keyComparator Comparator<? super U>
orders the keys
<T>
the compared type
<U>
the key type

Returns

the comparator

naturalOrder

public static <T extends Comparable<? super T>> Comparator<T> naturalOrder()
The natural order of comparable values.

Parameters

<T>
the compared type

Returns

the comparator

reverseOrder

public static <T extends Comparable<? super T>> Comparator<T> reverseOrder()
The reverse of the natural order.

Parameters

<T>
the compared type

Returns

the comparator

thenComparing

public default <U> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator)
This ordering, ties broken by a key in the order keyComparator gives.

Parameters

keyExtractor Function<? super T, ? extends U>
extracts the key
keyComparator Comparator<? super U>
orders the keys
<U>
the key type

Returns

the combined comparator

thenComparingInt

public default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor)
This ordering, ties broken by an int key.

Parameters

keyExtractor ToIntFunction<? super T>
extracts the key

Returns

the combined comparator

thenComparingLong

public default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor)
This ordering, ties broken by a long key.

Parameters

keyExtractor ToLongFunction<? super T>
extracts the key

Returns

the combined comparator

thenComparingDouble

public default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)
This ordering, ties broken by a double key, compared as Double.compare does.

Parameters

keyExtractor ToDoubleFunction<? super T>
extracts the key

Returns

the combined comparator

comparingInt

public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)
Compares by an int key.

Parameters

keyExtractor ToIntFunction<? super T>
extracts the key
<T>
the compared type

Returns

the comparator

comparingLong

public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor)
Compares by a long key.

Parameters

keyExtractor ToLongFunction<? super T>
extracts the key
<T>
the compared type

Returns

the comparator

comparingDouble

public static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor)
Compares by a double key, as Double.compare does: NaN above everything and -0.0 below 0.0.

Parameters

keyExtractor ToDoubleFunction<? super T>
extracts the key
<T>
the compared type

Returns

the comparator

nullsFirst

public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
Orders null before every other value, and the rest by comparator; with a null comparator the others are all equal.

Parameters

comparator Comparator<? super T>
orders the values that are not null, or null
<T>
the compared type

Returns

the comparator

nullsLast

public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)
Orders null after every other value, and the rest by comparator; with a null comparator the others are all equal.

Parameters

comparator Comparator<? super T>
orders the values that are not null, or null
<T>
the compared type

Returns

the comparator