Reverse Order Comparators in Java
This post will discuss reverse order comparators provided by JDK and implement our own reverse order comparators in Java. We will also cover how to handle null values in a Collection.
Comparators are the comparison functions applied to collections of objects to impose a total ordering on them. They can be passed to Collections.sort() or Arrays.sort() to allow precise control over the sort order.
We have already discussed natural order comparators in the previous post. Let’s discuss various reverse order comparators provided by JDK that compares objects in reverse order:
1. Using Comparator.reverseOrder() method
We can use static Comparator.reverseOrder() that returns a comparator that compares objects in reverse order. It imposes the reverse of the natural ordering.
Usage:
|
1 2 3 |
String[] s = { "B", "C", "A" }; Arrays.sort(s, Comparator.reverseOrder()); System.out.println(Arrays.toString(s)); // [C, B, A] |
The above code will throw a NullPointerException if the specified array contains any null value. Java provides two methods to handle nulls:
1. Using nullsFirst()
Comparator‘s nullsFirst() method will put all null values before all non-null values and apply specified ordering to all non-null elements, as shown below:
|
1 2 3 |
String[] s = { "B", null, "C", "A", null }; Arrays.sort(s, Comparator.nullsFirst(Comparator.reverseOrder())); System.out.println(Arrays.toString(s)); // [null, null, C, B, A] |
2. Using nullsLast()
Comparator‘s nullsLast() method will put all null values after all non-null values and apply specified ordering to all non-null elements, as shown below:
|
1 2 3 |
String[] s = { "B", null, "C", "A", null }; Arrays.sort(s, Comparator.nullsLast(Comparator.reverseOrder())); System.out.println(Arrays.toString(s)); // [C, B, A, null, null] |
2. Using Custom Comparators
We can also implement our own custom reverse order comparators:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.Comparator; class Main { public static void main(String[] args) { String[] s = { "B", "C", "A" }; Arrays.sort(s, new Comparator<String>() { @Override public int compare(String a, String b) { return b.compareTo(a); } }); System.out.println(Arrays.toString(s)); } } |
Output:
[C, B, A]
We can convert the above code to use generics:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.Comparator; class CustomComparator<T extends Comparable<T>> implements Comparator<T> { @Override public int compare(T a, T b) { return b.compareTo(a); } } class Main { public static void main(String[] args) { String[] s = { "B", "C", "A" }; Arrays.sort(s, new CustomComparator()); System.out.println(Arrays.toString(s)); } } |
Output:
[C, B, A]
In Java 8 and above, we can also replace comparators with lambda expressions:
|
1 2 3 |
String[] s = { "B", "C", "A" }; Arrays.sort(s, (a, b) -> b.compareTo(a)); System.out.println(Arrays.toString(s)); |
How can we handle nulls?
Like Comparator.reverseOrder(), the above code will throw a NullPointerException if the specified array contains any null value. We can easily modify the compare() method to handle nulls, as shown below:
1. To return a comparator 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 |
class CustomComparator<T extends Comparable<T>> implements Comparator<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); } } |
2. To return a comparator that considers null to be greater 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 |
class CustomComparator<T extends Comparable<T>> implements Comparator<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); } } |
3. Using Guava Library
Google’s Guava library provides static Ordering.natural().reverse() that returns an Ordering that uses the reverse order of the values.
Reverse Order Comparator in Java using Guava’s Ordering class
4. Using Apache Commons Collections
Apache commons-collections provides static ComparatorUtils.reversedComparator() that returns a comparator that reverses the order of the specified comparator.
That’s all about reverse order comparators in Java.
Reference: Comparator (Java Platform SE 8 )
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 :)