Comparator.naturalOrder() Method Example Java 8

We also have the natural comparator in Java 8, which will compare comparable objects. For this we get new naturalOrder() static method on Comparator Interface in Java 8.

Suppose if I want to compare Strings, well I can compare them in natural order, which is off course in the alphabetical order. Let see an example:

1.1) List Sorting Example Using Comparator.naturalOrder()

Output:

Also check post for creating Comparator using lambda expression in java 8, Comparator.comparing() method Example, Comparator.thenComparing() Method Example and handling null values on a Object property while creating Comparator using nullsFirst() and nullsLast() methods.

Reference:

Comparator.naturalOrder() Method in Java 8

Leave a Reply

Your email address will not be published. Required fields are marked *