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.

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.

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); integers.sort(Ordering.natural()); assertEquals(Arrays.asList(1,2,3), integers);

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

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>() { @Override public int compare(Person p1, Person p2) { return Ints.compare(p1.age, p2.age); } }; persons.sort(orderingByAge); assertEquals(Arrays.asList(new Person("Alice", 3), new Person("Michael", 10)), persons);

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

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 .natural() .nullsFirst() .onResultOf(new Function<Person, Comparable>() { @Override public Comparable apply(Person person) { return person.age; } }); persons.sort(ordering); assertEquals(Arrays.asList( 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.

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.