Guide to Guava’s Ordering – Guava’的订购指南

最后修改: 2017年 2月 2日

1. Overview


In this article, we will look at Ordering class from the Guava library.


The Ordering class implements the Comparator interface and gives us a useful fluent API for creating and chaining comparators.

Ordering 类实现了Comparator接口,为我们提供了一个有用的流畅的API来创建和连锁比较器。

As a quick sidenote, it’s also worth looking at the new Comparator.comparing() API – which provides similar functionality; here’s a practical example using that API.

作为一个快速的附带说明,也值得看看新的Comparator.comparing() API – 它提供了类似的功能;这里有一个使用该API的实际例子

2. Creating Ordering


Ordering has a useful builder method that returns a proper instance that can be used in a sort() method on collections or anywhere else where an instance of Comparator is needed.


We can create natural order instance by executing method natural():


List<Integer> integers = Arrays.asList(3, 2, 1);


assertEquals(Arrays.asList(1,2,3), integers);

Let’s say that we have a collection of Person objects:

比方说,我们有一个Person 对象的集合。

class Person {
    private String name;
    private Integer age;
    // standard constructors, getters

And we want to sort a list of such objects by age field. We can create our custom Ordering that will do exactly that by extending it:


List<Person> persons = Arrays.asList(new Person("Michael", 10), new Person("Alice", 3));
Ordering<Person> orderingByAge = new Ordering<Person>() {
    public int compare(Person p1, Person p2) {
        return, p2.age);


assertEquals(Arrays.asList(new Person("Alice", 3), new Person("Michael", 10)), persons);

Then we can use our orderingByAge and pass it to sort() method.

然后我们可以使用我们的orderingByAge ,并将其传递给sort() 方法。

3. Chaining Orderings


One useful feature of this class is that we can chain different ways of Ordering. Let’s say we have a collection of persons and we want to sort it by age field and have null age field values at the beginning of a list:


List<Person> persons = Arrays.asList(
  new Person("Michael", 10),
  new Person("Alice", 3), 
  new Person("Thomas", null));
Ordering<Person> ordering = Ordering
  .onResultOf(new Function<Person, Comparable>() {
      public Comparable apply(Person person) {
          return person.age;

  new Person("Thomas", null), 
  new Person("Alice", 3), 
  new Person("Michael", 10)), persons);

The important thing to notice here is an order in which particular Orderings are executed – order is from right to left. So firstly onResultOf() is executed and that method extracts the field that we want to compare.


Then, nullFirst() comparator is executed. Because of that, the resulting sorted collection will have a Person object that has a null as an age field at the beginning of the list.

然后,nullFirst()比较器被执行。因为这样,产生的排序集合将有一个Person 对象,它的null 作为age字段在列表的开头。

At the end of the sorting process, age fields are compared using natural ordering as specified using method natural().


4. Conclusion


In this article, we looked at Ordering class from Guava library that allows us to create more fluent and elegant Comparators. We created our custom Ordering, we used predefined ones from the API, and we chained them to achieve more custom order.


The implementation of all these examples and code snippets can be found in the GitHub project – this is a Maven-based project, so it should be easy to import and run as it is.