Comparator for pre-Java-8 users, in the same sense that FluentIterable is an
enriched Iterable for pre-Java-8 users.
The common ways to get an instance of Ordering are:
compare instead of implementing Comparator
directly
Comparator instance to from(Comparator)
Ordering#natural
Then you can use the chaining methods to get an altered version of that Ordering, including:
Finally, use the resulting Ordering anywhere a Comparator is required, or use
any of its special operations, such as:
Complex chained orderings like the following example can be challenging to understand.
Ordering<Foo> ordering =
Ordering.natural()
.nullsFirst()
.onResultOf(getBarFunction)
.nullsLast();
Note that each chaining method returns a new ordering instance which is backed by the previous
instance, but has the chance to act on values before handing off to that backing instance.
As a result, it usually helps to read chained ordering expressions backwards. For example,
when compare is called on the above ordering:
Foo is null, that null value is treated as greater
Foo values are passed to getBarFunction (we will be
comparing Bar values from now on)
Bar is null, that null value is treated as lesser
Bar.compareTo(Bar) is
returned)
Alas, reverse is a little different. As you read backwards through a chain and
encounter a call to reverse, continue working backwards until a result is determined, and
then reverse that result.
Except as noted, the orderings returned by the factory methods of this class are serializable
if and only if the provided instances that back them are. For example, if ordering and
function can themselves be serialized, then ordering.onResultOf(function) can as
well.
If you are using Java 8, this class is now obsolete. Most of its functionality is now provided
by Stream and by Comparator itself, and the rest can now
be found as static methods in our new Comparators class. See each method below for
further instructions. Whenever possible, you should change any references of type Ordering to be of type Comparator instead. However, at this time we have no plan to
deprecate this class.
Many replacements involve adopting Stream, and these changes can sometimes make your
code verbose. Whenever following this advice, you should check whether Stream could be
adopted more comprehensively in your code; the end result may be quite a bit simpler.
See the Guava User Guide article on Ordering.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| private static class | |
| pack-priv static class | Ordering.IncomparableValueException
Exception thrown by a |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final int | |
| pack-priv static final int |
| Access | Constructor and Description |
|---|---|
| protected | Ordering()
Constructs a new instance of this class (only invokable by the subclass constructor, typically implicit). |
| Modifier and Type | Method and Description |
|---|---|
| public static Ordering | allEqual()
Returns an ordering which treats all values as equal, indicating "no ordering." Passing this ordering to any stable sort algorithm results in no change to the order of elements. |
| public static Ordering | arbitrary()
Returns an arbitrary ordering over all objects, for which |
| public int | binarySearch(List<? extends T>
the list to be searched sortedList, T the key to be searched for key)
Deprecated
Use
Collections#binarySearch(List, Object, Comparator) directly.
Searches sortedList for
key using the binary search algorithm.
|
| public abstract int | compare(T
the first object to be compared. left, T the second object to be compared. right)Redeclares java. |
| public <U extends T> Ordering | compound(Comparator<? super U> secondaryComparator)
Returns an ordering which first uses the ordering |
| public static <T extends @Nullable Object> Ordering | compound(Iterable<? extends Comparator<? super T>>
the comparators to try in order comparators)Returns an ordering which tries each given comparator in order until a non-zero result is found, returning that result, and returning zero only if all comparators return zero. |
| public static <T> Ordering | |
| public static <T> Ordering | Returns: the comparator described abovethe value which the returned comparator should consider the "least" of all
values leastValue, T... the rest of the values that the returned comparator will be able
to compare, in the order the comparator should follow remainingValuesInOrder)Returns an ordering that compares objects according to the order in which they are given to this method. |
| public static <T extends @Nullable Object> Ordering | Returns: comparator itself if it is already anOrdering; otherwise an ordering that
wraps that comparatorthe comparator that defines the order comparator)Returns an ordering based on an existing comparator instance. |
| public static <T extends @Nullable Object> Ordering | |
| public <E extends T> List | Returns: an immutableRandomAccess list of the k greatest elements in
descending orderReturns the |
| public <E extends T> List | Returns: an immutableRandomAccess list of the k greatest elements in
descending orderReturns the |
| public <E extends @NonNull T> ImmutableList | immutableSortedCopy(Iterable<E> elements)
Returns an immutable list containing |
| public boolean | |
| public boolean | isStrictlyOrdered(Iterable<? extends T> iterable)
Returns |
| public <E extends T> List | |
| public <E extends T> List | |
| public <S extends T> Ordering | lexicographical()
Returns a new ordering which sorts iterables by comparing corresponding elements pairwise until a nonzero result is found; imposes "dictionary order". |
| public <E extends T> E | |
| public <E extends T> E | |
| public <E extends T> E | max(E
value to compare, returned if greater than or equal to b. a, E value to compare. b)Returns the greater of the two values according to this ordering. |
| public <E extends T> E | max(E
value to compare, returned if greater than or equal to the rest. a, E value to compare b, E value to compare c, E... values to compare rest)Returns the greatest of the specified values according to this ordering. |
| public <E extends T> E | |
| public <E extends T> E | |
| public <E extends T> E | min(E
value to compare, returned if less than or equal to b. a, E value to compare. b)Returns the lesser of the two values according to this ordering. |
| public <E extends T> E | min(E
value to compare, returned if less than or equal to the rest. a, E value to compare b, E value to compare c, E... values to compare rest)Returns the least of the specified values according to this ordering. |
| public static <C extends Comparable | |
| public <S extends T> Ordering | nullsFirst()
Returns an ordering that treats |
| public <S extends T> Ordering | nullsLast()
Returns an ordering that treats |
| pack-priv <T2 extends T> Ordering | |
| public <F extends @Nullable Object> Ordering | onResultOf(Function<F, ? extends T> function)
Returns a new ordering on |
| public <S extends T> Ordering | reverse()
Returns the reverse of this ordering; the |
| public <E extends T> List | sortedCopy(Iterable<E> elements)
Returns a mutable list containing |
| public static Ordering | usingToString()
Returns an ordering that compares objects by the natural ordering of their string
representations as returned by |