[Guava official documentation translation] 4. Use Guava Ordering to sort (Ordering Explained)

Source: Internet
Author: User
Tags iterable


OrderingIs the "smooth version" of Guava"ComparatorIt is used to build advanced comparator and apply comparator to various collections ).

OrderingThe instance is just a special


Natural () Natural sorting(Natural ordering ).
UsingToString () The return values of toString () are ordered alphabetically.


Ordering<String> byLengthOrdering =  Ordering<String> 

OrderingTo perform some preprocessing. Some of the most common solutions are as follows:

Reverse ()
NullsFirst () OrderingPlace the null element before the non-null element.Ordering is the same.The same is true for nullsLast.
Compound (Comparator) OrderingUse the specifiedComparatorSort again.
Lexicographical () OrderingThe iterable elements are ordered alphabetically.
OnResultOf (Function) OrderingPass the sorted elements into the function and use the originalOrdering sorting.



Processing null values based on sortedBy. The following is a solution for application chain call:


Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(newFunction<Foo,String>


 In Ordering.

The chain call of compound should be viewed from left to right. To avoid confusion, do not CompoundMixed processing of calls and other chained calls .)

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

GreatestOf (Iterable iterable, int k) LeastOf
IsOrdered (Iterable) IterableWhether or not the ordering sorting rules are ordered (monotonic non-subtraction ). IsStrictlyOrdered
SortedCopy (Iterable)
Min (E, E) Max (E, E)
Min (E, E ...) The smallest of the two parameters is returned Based on the ordering sorting rule. If there are multiple equal minimum values, the first value is returned.
Max (E, E ...)
Min (Iterable) The minimum Iterable value. IfThisIterableIf it is null, return 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.