[Technical translation] Guava official document Ordering

Source: Internet
Author: User

About sorting

Example

AssertTrue (byLengthOrdering. reverse (). isOrdered (list ));

Overview

Ordering is a chain comparator class of Guava. It can be used to construct a complex comparator and apply it to collection objects.

In essence, it is only a special comparator instance. Ordering only executes methods that depend on a comparator (such as Collections. max) and uses these methods as instance methods. The Ordering class provides some methods to adjust and enhance the existing comparator.

How to CreateOrdering

Common orderings are created by the following static method.

Method

Description

Natural ()

Sort comparable objects in natural order

UsingToString ()

Sort the strings returned by toString () in alphabetical order.

Ordering. from (Comparator)

Use an existing Comparator

However, the more general way to create an Ordering is to skip Comparator and inherit an Ordering abstract class directly.

 

Chain comparison

 Ordering<String> byLengthOrdering =  Ordering<String>           };

 

The Ordering method can be encapsulated to obtain the derived Orderings. Below are some frequently used variant methods.

 

 

 

Method

Description

Reverse ()

Returns an Ordering.

NullsFirst ()

Return an Ordering. non-null value first. The rest is the same as the original Ordering.

Compound (Comparator)

When the first comparator is equal, the second comparator is used.

Lexicographical ()

Returns an Ordering that orders iterables lexicographically by their elements.

Returns a comparator that traverses its elements in alphabetical order.

OnResultOf (Function)

Returns a comparator that applies the function to its elements and sorts the elements according to the original comparator.

For example, if you want a comparator that can compare the following classes...

      }

 

It can process empty sortedBy. Here is a solution built on the chained method.

 Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf( Function<Foo, String>          });

 

When reading a Chain Ordering., you can see it from the right to the left. In the above example, the Foo instance is sorted by sortedBy, which finds their property values. First, move non-null values to the top, and then sort the remaining values. The reason is that every chain call is to wrap the previous Ordering into a new Ordering.

(A special case of the forward-looking rule from the back: if there is a compound in the chain, read it from the left to the back. To avoid confusion, do not mix calls that contain compound with other chained calls .)

The long chain is not easy to understand. We recommend that you limit the chain to Three callbacks as in the example. Even so, you can simplify the chain by splitting intermediate objects like Function instances.

 Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(sortKeyFunction);

 

About applications

Guava provides many methods to process and check values or sets using ordering. We will list the following frequently used items below.

Method

Description

See also

GreatestOf (Iterable iterable, int k)

Returns the k greatest elements of the specified iterable, according to this ordering, in order from greatest to least. Not necessarily stable.

LeastOf

IsOrdered (Iterable)

Tests if the specified Iterable is in nondecreasing order according to this ordering.

IsStrictlyOrdered

SortedCopy (Iterable)

Returns a sorted copy of the specified elements as a List.

ImmutableSortedCopy

Min (E, E)

Returns the minimum of its two arguments according to this ordering. If the values compare as equal, the first argument is returned.

Max (E, E)

Min (E, E ...)

Returns the minimum of its arguments according to this ordering. If there are multiple least values, the first is returned.

Max (E, E ...)

Min (Iterable)

Returns the minimum element of the specified Iterable. Throws a NoSuchElementException if theIterable is empty.

Max (Iterable), min (Iterator), max (Iterator)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.