Natural Order Comparators in Java

In this post, we will cover natural order comparators provided by JDK and also write our own natural order comparators. We will also cover 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 on them. They can be passed to Collections.sort() or Arrays.sort() to allow precise control over the sort order. They can also be used 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 natural order –

 

1. Comparator.naturalOrder()

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

Usage:

Above code will throw a NullPointerException if specified array contains any null value. Java provides two methods to handle nulls –

 

1. 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 to be equal. i.e. non-null values will remain unsorted a shown below.

 

2. 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. Custom Comparators

We can also write our own custom natural order comparators:

Java

Download   Run Code

Output:

[A, B, C]

Above code can be converted to use generics:

Java

Download   Run Code

Output:

[A, B, C]

We can also replace comparator by Lambda expressions (in Java 8 and above) –

 
How can we handle nulls?

Like Comparator.naturalOrder(), above code will throw a NullPointerException if specified array contains any null value. We can easily modify compare() function 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. Java 8 – Comparator.comparing()

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

Usage:

 
We can also replace the identity function by corresponding lambda expression –

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

 

4. Guava

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

Refer: Natural Comparators by Guava Ordering Class

 

5. Apache commons collections

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

Refer: Natural Comparators in Apache Commons Collections

 
Related Article: Reverse Order Comparators in Java

 
 
References: Comparator (Java Platform SE 8 )

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz