Reverse Order Comparators by Guava’s Ordering class in Java
We have already discussed the reverse order comparator provided by JDK in the previous post. This post will discuss the reverse order comparator provided by Guava Ordering class in Java.
Guava’s Ordering class allows us to build complex comparators and apply them to collections of objects. It also provides many useful methods to sort, filter, and manipulate data based on the ordering. Basically Ordering is Guava’s Comparator class with additional methods to support common operations not provided by JDK.
1. Using Ordering.natural().reverse() method
Guava’s Ordering class is a powerful and flexible way to chain comparators in Java. It provides the natural() method that returns an Ordering object that uses the natural order of the values and the reverse() method that returns an Ordering object that uses the reverse order of the original Ordering. Therefore, we can use Ordering.natural().reverse() to get an ordering that uses the reverse of the natural order of the values. We can use this Ordering object to sort collections in reverse order. For example, we can sort an array of strings in reverse order like this:
|
1 2 3 4 5 6 7 8 9 10 11 |
import com.google.common.collect.Ordering; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", "C", "A" }; Arrays.sort(s, Ordering.natural().reverse()); System.out.println(Arrays.toString(s)); // [C, B, A] } } |
The above code will throw a NullPointerException if the specified array contains any null value. We can handle nulls using the nullsFirst() method that returns an ordering that considers null to be less than non-null values, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.collect.Ordering; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; // put all null values before non-null values Arrays.sort(s, Ordering.natural().reverse().nullsFirst()); // [null, null, C, B, A] System.out.println(Arrays.toString(s)); } } |
Alternatively, we can use the nullsLast() method that returns an ordering that considers null to be greater than the non-null values, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.collect.Ordering; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; // put all null values after all non-null values Arrays.sort(s, Ordering.natural().reverse().nullsLast()); // [C, B, A, null, null] System.out.println(Arrays.toString(s)); } } |
2. Using Custom Ordering
Guava’s Ordering class implements the Comparator interface and provides many methods to customize the ordering of objects. We can also create a custom reverse order comparator by extending the Ordering class and overriding the compare() method to implement our own logic for reversing the order. Here is an example of how to create a custom reverse order comparator using Guava’s Ordering class:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.common.collect.Ordering; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", "C", "A" }; Arrays.sort(s, new Ordering<>() { @Override public int compare(String a, String b) { return b.compareTo(a); } }); System.out.println(Arrays.toString(s)); // [C, B, A] } } |
The above custom implementation will throw a NullPointerException if the specified array contains any null value. We can easily modify the compare() method to handle nulls. For example, consider the following generic version, that returns an Ordering that considers null to be less than non-null values:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import com.google.common.collect.Ordering; import java.util.Arrays; class ReverseOrdering<T extends Comparable<T>> extends Ordering<T> { @Override public int compare(T a, T b) { // return 0 if both `a` and `b` are null if (a == null && b == null) { return 0; } // return -1 if `a` is null and `b` is not null if (a == null) { return -1; } // return 1 if `b` is null and `a` is not null if (b == null) { return 1; } // if both `a` and `b` are not null return b.compareTo(a); } } class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; // Sort the array using the custom reverse order comparator Arrays.sort(s, new ReverseOrdering<>()); System.out.println(Arrays.toString(s)); // [null, null, C, B, A] } } |
To return an Ordering that considers null to be greater than non-null values, we can do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import com.google.common.collect.Ordering; import java.util.Arrays; class ReverseOrdering<T extends Comparable<T>> extends Ordering<T> { @Override public int compare(T a, T b) { // return 0 if both `a` and `b` are null if (a == null && b == null) { return 0; } // return 1 if `a` is null and `b` is not null if (a == null) { return 1; } // return -1 if `b` is null and `a` is not null if (b == null) { return -1; } // if both `a` and `b` are not null return b.compareTo(a); } } class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; Arrays.sort(s, new ReverseOrdering<>()); System.out.println(Arrays.toString(s)); // [C, B, A, null, null] } } |
That’s all about reverse order comparators provided by Guava’s Ordering class in Java.
Related Post:
Reference: Ordering (Guava: Google Core Libraries for Java 31.0-jre API)
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)