We have already discussed natural comparators provided by JDK in previous post. In this post, we will discuss natural comparators provided by Apache commons collections.
Apache commons collections provides static ComparatorUtils.naturalComparator()
that returns a comparator that uses the natural order of the objects.
Usage:
1 2 3 |
String[] s = { "B", "C", "A" }; Arrays.sort(s, ComparatorUtils.naturalComparator()); System.out.println(Arrays.toString(s)); // [A, B, C] |
Apache commons collections also provides ComparatorUtils.NATURAL_COMPARATOR
that provides a comparator for natural sort order of the objects.
It is equivalent to ComparatorUtils.naturalComparator()
.
Usage:
1 2 3 |
String[] s = { "B", "C", "A" }; Arrays.sort(s, ComparatorUtils.NATURAL_COMPARATOR); System.out.println(Arrays.toString(s)); // [A, B, C] |
If specified array contains any null value, ComparatorUtils.naturalComparator()
will throw a NullPointerException
. Apache commons collections provides two methods to handle nulls –
1. nullLowComparator()
ComparatorUtils
class provides static nullLowComparator()
method that returns a comparator that consider 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())); // [null, null, A, B, C] System.out.println(Arrays.toString(s)); |
or
1 2 3 |
String[] s = { "B", null, "C", "A", null }; Arrays.sort(s, ComparatorUtils.nullLowComparator(null)); System.out.println(Arrays.toString(s)); // [null, null, A, B, C] |
2. nullHighComparator()
ComparatorUtils
class also provides static nullHighComparator()
method that returns a comparator that consider 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())); // [A, B, C, null, null] System.out.println(Arrays.toString(s)); |
or
1 2 3 |
String[] s = { "B", null, "C", "A", null }; Arrays.sort(s, ComparatorUtils.nullHighComparator(null)); System.out.println(Arrays.toString(s)); // [A, B, C, null, null] |
Apache commons collections also provides NullComparator
class whose instance will sorts null higher or lower than any non-null object it is compared with.
1. Nulls in beginning
1 2 3 4 5 6 7 8 |
String[] s = { "B", null, "C", "A", null }; // Construct an instance that sorts null lower than any non-null // object it is compared with Arrays.sort(s, new NullComparator(false)); // [null, null, A, B, C] System.out.println(Arrays.toString(s)); |
2. Nulls in the end
1 2 3 4 5 6 7 8 |
String[] s = { "B", null, "C", "A", null }; // Construct an instance that sorts null higher than any non-null // object it is compared with. Arrays.sort(s, new NullComparator(true)); // or new NullComparator() // [A, B, C, null, null] System.out.println(Arrays.toString(s)); |
Related Posts: Natural Comparators by Guava Ordering Class
References:
1. ComparatorUtils (Apache Commons Collections 4.1 API)
2. NullComparator (Apache Commons Collections 4.1 API)
Thanks for reading.
Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
Leave a Reply