This post will discuss natural order comparators provided by JDK and write our own natural order comparators in Java. We will also see how to handle null values in a Collection or array.

Comparators are the comparison functions that can be applied to collections of objects or an array to impose a total ordering. They can be passed to Collections.sort() or Arrays.sort() to allow precise control over the sort order. We can also use them to provide an ordering for collections of objects that don’t have a natural ordering.

 
Let’s discuss various natural order comparators provided by JDK that compares objects in the natural order:

1. Using Comparator.naturalOrder() method

We can use static Comparator.naturalOrder() that returns a comparator that compares objects in the natural order.

Usage:

 
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 takes another comparator and returns a comparator that considers null to be less than non-null values. If passed to a sort method, the comparator will put all null values before all non-null values and apply natural ordering to all non-null elements, as shown below:

 
If the specified comparator is null, then the returned comparator considers all non-null values equal, i.e., non-null values will remain unsorted a shown below.

2. Using nullsLast()

Comparator‘s nullsLast() method takes another comparator and returns a comparator that considers null to be greater than non-null values. If passed to a sort method, the comparator will put all null values after all non-null values and apply natural ordering to all non-null elements, as shown below:

 
If the specified comparator is null, then the returned comparator considers all non-null values to be equal, i.e., non-null values will remain unsorted:

2. Using Custom Comparators

We can also write our own custom natural order comparators:

Download  Run Code

Output:

[A, B, C]

 
We can convert the above code to use generics:

Download  Run Code

Output:

[A, B, C]

 
We can also replace the comparator with lambda expressions (in Java 8 and above):

How can we handle nulls?

Like Comparator.naturalOrder(), 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:

 
2. To return a comparator that considers null to be greater than non-null values:

3. Using Comparator.comparing() method

Java 8 has brought several enhancements to the Comparator interface, such as introducing a new static method comparing(). We can pass an instance method reference to the Comparator.comparing() method, and it will extract and returns a comparator based on that function.

Usage:

 
We can also replace the identity method by the corresponding lambda expression:

 
This method also throws a NullPointerException if the specified array contains any null value. As seen in the previous sections, we can use nullsFirst() or nullsLast() to handle null values:

4. Using Guava Library

Google’s Guava library provides static Ordering.natural() that returns an Ordering that uses the natural order of the values.

Natural order comparators in Java by Guava’s Ordering class

5. Using Apache Commons Collections

Apache commons-collections provides static ComparatorUtils.naturalComparator() that returns a natural order comparator.

Natural Order Comparators by Apache Commons Collections in Java

That’s all about natural order comparators in Java.

 
Related Article:

Reverse Order Comparators in Java

 
Reference: Comparator (Java Platform SE 8 )