Java 8 – Comparator.naturalOrder() and Comparator.reverseOrder()

In this article, we will discuss how to sort list using Java 8 Comparator’s static methods – naturalOrder() and reverseOrder()

1. Comparator.naturalOrder() method :

  • Returns a comparator that compares Comparable objects in natural order
  • In other words, returns a comparator that imposes the natural ordering on Comparable objects
  • The returned comparator is serializable
  • Throws NullPointerException when comparing null
  • Method signature :-
    • static <T extends Comparable<? super T>> Comparator<T> naturalOrder()
  • Where
    • T is the Comparable type of element to be compared



2. Comparator.reverseOrder() method :

  • Returns a comparator that imposes the reverse of the natural ordering
  • In other words, returns a comparator that imposes the reverse of the natural ordering on Comparable objects
  • The returned comparator is serializable
  • Throws NullPointerException when comparing null
  • Method signature :-
    • static <T extends Comparable<? super T>> Comparator<T> reverseOrder()
  • Where
    • T is the Comparable type of element to be compared

3. naturalOrder() and reverseOrder() examples :

3.1 To sort list of integer numbers in ascending/descending order

  • A list contains integer numbers in random order
  • Ascending order – we are going to sort integer numbers in natural order (or ascending-order) using Comparator.naturalOrder() comparator
  • Descending order – we are going to sort integer numbers in decreasing order (or descending-order) using Comparator.reverseOrder() comparator

SortingIntegerList.java

package net.bench.resources.java8.comparator.sorting;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class SortingIntegerList {

	public static void main(String[] args) {

		// 1. List of integer numbers
		List<Integer> numbers = Arrays.asList(
				63, 19, 86, 97, 7, 10, 67
				);


		// 1.1 print to console
		System.out.println("Original numbers as per Insertion-order :- \n");
		numbers.forEach(System.out::println); // iterating/printing



		// 2. sorting in ascending order
		System.out.println("\nAscending order sorted numbers"
				+ " using Comparator.naturalOrder() :- \n");


		numbers // original data source
		.stream() // 1. get sequential stream
		.sorted(Comparator.naturalOrder()) // 2. natural sorting
		.forEach(System.out::println); // 3. iterating/printing



		// 3. sorting in descending order
		System.out.println("\nDescending order sorted numbers"
				+ " using Comparator.reverseOrder() :- \n");


		numbers // original data source
		.stream() // 1. get sequential stream
		.sorted(Comparator.reverseOrder()) // 2. reverse sorting
		.forEach(System.out::println); // 3. iterating/printing
	}
}

Output:

Original numbers as per Insertion-order :- 

63
19
86
97
7
10
67

Ascending order sorted numbers using Comparator.naturalOrder() :- 

7
10
19
63
67
86
97

Descending order sorted numbers using Comparator.reverseOrder() :- 

97
86
67
63
19
10
7

3.2 To sort list of String elements in alphabetical/reverse order

  • A list contains String elements in random order
  • Alphabetical order – we are going to sort list of String elements in alphabetical order using Comparator.naturalOrder() comparator
  • Reverse order – we are going to sort list of String elements in reverse alphabetical order using Comparator.reverseOrder() comparator

SortingStringList.java

package net.bench.resources.java8.comparator.sorting;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class SortingStringList {

	public static void main(String[] args) {

		// 1. List of String elements
		List<String> names = Arrays.asList(
				"Salman Khan",
				"Ranbir Kapoor",
				"Aamir Khan",
				"Hrithik Roshan",
				"Akshay Kumar",
				"Sharukh Khan",
				"Ranveer Singh"
				);


		// 1.1 print to console
		System.out.println("Names as per insertion order in List) :- \n");
		names.forEach(System.out::println); // iterating/printing



		// 2. sorting in ascending order
		System.out.println("\nAlphabetically sorted names"
				+ " using Comparator.naturalOrder() :- \n");

		names // original data source
		.stream() // 1. get sequential stream
		.sorted(Comparator.naturalOrder()) // 2. natural sorting
		.forEach(System.out::println); // 3. iterating/printing


		// 3. sorting in descending order
		System.out.println("\nReverse alphabetically sorted names"
				+ " using Comparator.naturalOrder() :- \n");


		names // original data source
		.stream() // 1. get sequential stream
		.sorted(Comparator.reverseOrder()) // 2. reverse sorting
		.forEach(System.out::println); // 3. iterating/printing
	}
}

Output:

Names as per insertion order in List) :- 

Salman Khan
Ranbir Kapoor
Aamir Khan
Hrithik Roshan
Akshay Kumar
Sharukh Khan
Ranveer Singh

Alphabetically sorted names using Comparator.naturalOrder() :- 

Aamir Khan
Akshay Kumar
Hrithik Roshan
Ranbir Kapoor
Ranveer Singh
Salman Khan
Sharukh Khan

Reverse alphabetically sorted names using Comparator.naturalOrder() :- 

Sharukh Khan
Salman Khan
Ranveer Singh
Ranbir Kapoor
Hrithik Roshan
Akshay Kumar
Aamir Khan

References:

Happy Coding !!
Happy Learning !!

Java 8 - Comparator.reversed()
Java 8 – How to sort TreeMap entries in descending order ?