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:
// Java program to demonstrate // Comparator.naturalOrder() method 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); // naturalOrder is a static method values.sort(Comparator.naturalOrder()); // print sorted number based on natural order System.out.println(values); } } |
The output printed on console of IDE is shown below.
Output:

Program 2:
// Java program to demonstrate // Comparator.naturalOrder() method 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()
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java | Collectors maxBy(Comparator comparator) with Examples
- Java | Collectors minBy(Comparator comparator) with Examples
- Stream sorted (Comparator comparator) method in Java
- TreeMap comparator() method in Java with Examples
- SortedMap comparator() method in Java with Examples
- ConcurrentSkipListSet comparator() method in Java with Examples
- Comparator nullsFirst() method in Java with examples
- Comparator nullsLast() method in Java with examples
- Comparator thenComparingInt() method in Java with examples
- Comparator reverseOrder() method in Java with examples
- Comparator reversed() method in Java with examples
- Comparator comparingLong() method in Java with examples
- Comparator thenComparingDouble() method in Java with examples
- Comparator comparingDouble() method in Java with examples
- Comparator thenComparingLong() method in Java with examples
- Comparator comparingInt() in Java with examples
- Comparator Interface in Java with Examples
- TreeSet comparator() Method in Java
- PriorityQueue comparator() Method in Java
- PriorityBlockingQueue comparator() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

