The naturalOrder() method of Comparator Interface in Java returns a comparator that use to compare Comparable objects in natural order. The returned comparator by this method is serializable and throws NullPointerException when comparing null.
Syntax:
static <T extends Comparable<T>>
Comparator<T> naturalOrder()
Parameters: This method accepts nothing.
Return value: This method returns a comparator that imposes the natural ordering on Comparable objects.
Below programs illustrate naturalOrder() method:
Program 1:
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class GFG {
public static void main(String... args)
{
List<Integer> values
= Arrays.asList( 212 , 324 ,
435 , 566 ,
133 , 100 , 121 );
values.sort(Comparator.naturalOrder());
System.out.println(values);
}
}
|
The output printed on console of IDE is shown below.
Output:

Program 2:
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class GFG {
public static void main(String... args)
{
List<String> stringList
= Arrays.asList( "Aman" , "Kajal" ,
"Joyita" , "Das" );
System.out.println( "Before sorting:" );
stringList.forEach(System.out::println);
stringList.sort(Comparator.naturalOrder());
System.out.println( "\nAfter sorting:" );
stringList.forEach(System.out::println);
}
}
|
The output printed on console is shown below.
Output:

References: https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html#naturalOrder()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Apr, 2019
Like Article
Save Article