Reverse Order Comparators in Apache Commons Collections
We have already discussed the reverse order comparators provided by JDK in the previous post. This post will discuss the reverse order comparators provided by Apache commons-collections in Java.
Apache commons-collections provides static ComparatorUtils.reversedComparator() that returns a comparator that reverses the order of the specified comparator. Since we want a reverse order comparator, we can pass any Natural Order Comparator to it.
Usage:
|
1 2 3 |
String[] s = { "B", "C", "A" }; Arrays.sort(s, ComparatorUtils.reversedComparator(ComparatorUtils.NATURAL_COMPARATOR); System.out.println(Arrays.toString(s)); // [C, B, A] |
We can also call Comparator.reversed() on ComparatorUtils.NATURAL_COMPARATOR to get comparator that imposes the reverse ordering. It is equivalent to calling Collections.reverseOrder(ComparatorUtils.NATURAL_COMPARATOR).
Usage:
|
1 2 3 |
String[] s = { "B", "C", "A" }; Arrays.sort(s, ComparatorUtils.NATURAL_COMPARATOR.reversed()); System.out.println(Arrays.toString(s)); // [C, B, A] |
If the specified array contains any null value, ComparatorUtils.reversedComparator() will throw a NullPointerException. Apache commons-collections provides two methods to handle nulls:
1. Using nullLowComparator() method
ComparatorUtils class provides static nullLowComparator() method that returns a comparator that considers a null value to be less than any non-null value and equal to any other null value:
|
1 2 3 4 5 6 7 8 |
String[] s = { "B", null, "C", "A", null }; // put all null values before non-null values Arrays.sort(s, ComparatorUtils.nullLowComparator(ComparatorUtils .naturalComparator().reversed())); // [null, null, C, B, A] System.out.println(Arrays.toString(s)); |
2. Using nullHighComparator() method
ComparatorUtils class also provides static nullHighComparator() method that returns a comparator that considers a null value to be greater than any non-null value and equal to any other null value:
|
1 2 3 4 5 6 7 8 |
String[] s = { "B", null, "C", "A", null }; // put all null values after all non-null values Arrays.sort(s, ComparatorUtils.nullHighComparator(ComparatorUtils .naturalComparator().reversed())); // [C, B, A, null, null] System.out.println(Arrays.toString(s)); |
Apache commons-collections also provides NullComparatorclass whose instance will sort null higher or lower than any non-null object it is compared with.
⮚ Nulls at the beginning
|
1 2 3 4 |
String[] s = { "B", null, "C", "A", null }; Arrays.sort(s, new NullComparator(ComparatorUtils.NATURAL_COMPARATOR.reversed(), false)); System.out.println(Arrays.toString(s)); // [null, null, C, B, A] |
⮚ 2. Nulls at the end
|
1 2 3 4 |
String[] s = { "B", null, "C", "A", null }; Arrays.sort(s, new NullComparator(ComparatorUtils.NATURAL_COMPARATOR.reversed())); System.out.println(Arrays.toString(s)); // [C, B, A, null, null] |
That’s all about reverse order comparators in Apache Commons Collections.
Related Post:
Reverse Order Comparator in Java using Guava’s Ordering class
References:
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 :)