Home  >  Java 8

Java Comparator.naturalOrder

By Arvind Rai, March 22, 2019
naturalOrder is the static method of Comparator functional interface. Comparator.naturalOrder method introduced in Java 8, returns a comparator that compares Comparable objects in natural order. For natural ordering, a class needs to implement Comparable and define compareTo method. A collection of objects are sorted according to compareTo method in natural ordering. Java classes such as Integer, String and Date implement Comparable interface and override its compareTo method and they are sorted in lexicographic-order.
Find the naturalOrder method declaration from Java source code.
static <T extends Comparable<? super T>> Comparator<T> naturalOrder() 
To reverse natural ordering, we can use Comparator.reverseOrder method. Here on this page we will provide examples to use Comparator.naturalOrder with Stream.sorted, Collections.sort, List.sort and Arrays.sort to sort the collections of objects in natural order.

Comparator.naturalOrder with Stream.sorted

Stream.sorted returns a stream consisting of the elements of this stream, sorted according to the provided comparator.
StreamSortedDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class StreamSortedDemo {
  public static void main(String[] args) {
	List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
	numList.stream().sorted(Comparator.naturalOrder()).forEach(n -> System.out.print(n + " "));
	System.out.println("\n-----------");
	
	List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida");
	strList.stream().sorted(Comparator.naturalOrder()).forEach(s -> System.out.print(s + " "));
	System.out.println("\n-----------");	
	
	List<Student> stdList = Student.getStudentList();
	stdList.stream().sorted(Comparator.naturalOrder()).forEach(s -> System.out.print(s.getName() + " "));
	System.out.println("\n-----------");	
	
	//reverse order of natural order using Comparator.reverseOrder()
	stdList = Student.getStudentList();
	stdList.stream().sorted(Comparator.reverseOrder()).forEach(s -> System.out.print(s.getName() + " "));	
  }
} 
Student.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class Student implements Comparable<Student> {
  private String name;
  private int age;
  public Student(String name, int age) {
	this.name = name;
	this.age = age;
  }
  public String getName() {
	return name;
  }
  public int getAge() {
	return age;
  }
  @Override
  public int compareTo(Student s) {
	return name.compareTo(s.getName());
  }
  public static List<Student> getStudentList() {
	Student s1 = new Student("Ram", 18);
	Student s2 = new Student("Shyam", 22);
	Student s3 = new Student("Mohan", 19);
	Student s4 = new Student("Mahesh", 20);
	Student s5 = new Student("Krishna", 21);
	List<Student> list = Arrays.asList(s1, s2, s3, s4, s5);
	return list;
  }
} 
Output
8 10 11 12 15 
-----------
Allahabad Kanpur Noida Varanasi 
-----------
Krishna Mahesh Mohan Ram Shyam 
-----------
Shyam Ram Mohan Mahesh Krish 

Comparator.naturalOrder with Collections.sort

Collections.sort sorts the specified list according to the given Comparator instance.
CollectionsSortDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionsSortDemo {
  public static void main(String[] args) {
	List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
	Collections.sort(numList, Comparator.naturalOrder());
	numList.forEach(n -> System.out.print(n + " "));
	System.out.println("\n-----------");
	
	List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida");
	Collections.sort(strList, Comparator.naturalOrder());
	strList.forEach(s -> System.out.print(s + " "));
	System.out.println("\n-----------");	
	
	List<Student> stdList = Student.getStudentList();
	Collections.sort(stdList, Comparator.naturalOrder());
	stdList.forEach(s -> System.out.print(s.getName() + " "));	
  }
} 

Comparator.naturalOrder with List.sort

List.sort sorts this list according to the given Comparator instance.
ListSortDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ListSortDemo {
  public static void main(String[] args) {
	List<Integer> numList = Arrays.asList(12, 10, 15, 8, 11);
	numList.sort(Comparator.naturalOrder());
	numList.forEach(n -> System.out.print(n + " "));
	System.out.println("\n-----------");
	
	List<String> strList = Arrays.asList("Varanasi", "Allahabad", "Kanpur", "Noida");
	strList.sort(Comparator.naturalOrder());
	strList.forEach(s -> System.out.print(s + " "));
	System.out.println("\n-----------");	
	
	List<Student> stdList = Student.getStudentList();
	stdList.sort(Comparator.naturalOrder());
	stdList.forEach(s -> System.out.print(s.getName() + " "));
  }
} 

Comparator.naturalOrder with Arrays.sort

Arrays.sort sorts the specified array of objects according to the order induced by the specified comparator.
ArraysSortDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.Comparator;
public class ArraysSortDemo {
  public static void main(String[] args) {
	Student s1 = new Student("Ram", 18);
	Student s2 = new Student("Shyam", 22);
	Student s3 = new Student("Mohan", 19);

	Student[] stdArray = { s1, s2, s3 };
	Arrays.sort(stdArray, Comparator.naturalOrder());
	for (Student s : stdArray) {
	  System.out.print(s.getName() + " ");
	}
  }
} 

References

Interface Comparator
Java 8 Stream sorted() Example
Java Comparator.reverseOrder
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us