Comparator naturalOrder() method in Java with examples Last Updated : 29 Apr, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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 // 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() Comment More infoAdvertise with us Next Article Comparator naturalOrder() method in Java with examples A AmanSingh2210 Follow Improve Article Tags : Java Java - util package Java-Functions Java-Comparator Practice Tags : Java Similar Reads Comparator reverseOrder() method in Java with examples The reverseOrder() method of Comparator Interface in Java returns a comparator that use to compare Comparable objects in reverse of natural order. The returned comparator by this method is serializable and throws NullPointerException when comparing null. Syntax: static <T extends Comparable<T 1 min read TreeMap comparator() method in Java with Examples The comparator() method of java.util.TreeMap class is used to return the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys. --> java.util Package --> TreeMap Class --> comparator() Method Syntax: public Comparator comparator() Return Ty 2 min read TreeSet comparator() Method in Java with Examples TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to co 3 min read Comparator reversed() method in Java with examples The reversed() method of Comparator Interface in Java returns a comparator that imposes the reverse ordering of this comparator. If you use sort method of the array and passes this comparator after applying the reversed method then it will sort the array in reverse order. Syntax: default Comparator 2 min read Comparator nullsFirst() method in Java with examples The nullsFirst(java.util.Comparator) method returns comparator that is a null-friendly comparator and considers null values to be less than non-null. The null first operates by the following logic: The null element is considered to be less than non-null. When both elements are null, then they are co 3 min read SortedMap comparator() method in Java with Examples The comparator() method of java.util.SortedMap interface is used to return the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.Syntax: public Comparator comparator() Return Value: This method returns the comparator used to order the keys in th 2 min read Comparator nullsLast() method in Java with examples The nullsLast (java.util.Comparator) method returns comparator that is a null-friendly comparator and considers null values greater than non-null. The null first operates by the following logic: The null element is considered to be greater than non-null. When both elements are null, then they are co 3 min read Comparator thenComparingLong() method in Java with examples The thenComparingLong(java.util.function.ToLongFunction) method of Comparator Interface in Java returns a lexicographic-order comparator with a function that extracts a Long sort key. Syntax: default Comparator <T> thenComparingLong( ToLongFunction <T> keyExtractor) Parameters: This meth 2 min read Short compareTo() method in Java with Examples The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically. Syntax: public int compareTo(Short otherShort) Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared. Return type : It return 2 min read Comparator thenComparingDouble() method in Java with examples The thenComparingDouble(java.util.function.ToDoubleFunction) method of Comparator Interface in Java returns a lexicographic-order comparator with a function that extracts a double sort key. This method is applied after comparing method if you want to apply another comparing for those values which ar 3 min read Like