Java 8 – Comparator.reversed() method

In this article, we will discuss how to sort list using Java 8 Comparator’s default method – reversed() which returns a comparator for sorting in reverse ordering

1. Comparator.reversed() method :

  • Returns a comparator that imposes the reverse ordering of this comparator
  • This is the default method defined in Comparator interface
  • Method signature :-
    • default Comparator<T> reversed()
  • Where
    • T is the Comparable type of element to be compared

2. Comparator.reversed() examples :

  • This is the default method introduced in Java 8 Comparator interface to get reverse of the original Comparator
  • For example, we have got a Comparator which sorts a list in increasing order (or ascending-order) then we can simply invoke reversed() method on this original Comparator to get reverse Comparator which sorts in decreasing order (or descending-order)

Product.java

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

public class Product {

	// member variables
	private int id;
	private String name;
	private long quantity;
	private double price;

	// 4-arg parameterized constructor

	// getters and setters

	// toString() method
}

2.1 To sort Product list in decreasing order of its price

  • A list contains Product information for 5 as per insertion-order
  • First, we are going to sort the Product list on the basis of increasing price
  • Second, we are going to sort the Product list on the basis of decreasing price
package net.bench.resources.java8.comparator.sorting;

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

public class SortingProductListWithPrice {

	// List of Products
	private static List<Product> getProductList() {

		return Arrays.asList(
				new Product(101, "Wheat", 1089L, 36.89),
				new Product(102, "Rice", 502L, 58.19),
				new Product(103, "Lentils", 803L, 102.45),
				new Product(104, "Oil", 208L, 164.75),
				new Product(105, "Vegetables", 303L, 45.50)
				);
	}

	public static void main(String[] args) {

		// 1. get Product list
		List<Product> products = getProductList();


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



		// 2. sorting in ascending order of Product price
		System.out.println("\nIncreasing price - Sorted Product list :- \n");

		// 2.1 get comparator for sorting according to increasing Price
		Comparator<Product> priceComparatorAsc = Comparator
				.comparing(Product::getPrice); // increasing order


		products // original data source
		.stream() // 1. get sequential stream
		.sorted(priceComparatorAsc) // 2. sorting increasing Price
		.forEach(System.out::println); // 3. iterating/printing



		// 3. sorting in descending order of Product price
		System.out.println("\nDecreasing price - Sorted Product list :- \n");


		// 3.1 get comparator for sorting according to decreasing Price
		Comparator<Product> priceComparatorDesc = Comparator
				.comparing(Product::getPrice) // increasing order
				.reversed(); // decreasing order


		products // original data source
		.stream() // 1. get sequential stream
		.sorted(priceComparatorDesc) // 2. sorting decreasing Price
		.forEach(System.out::println); // 3. iterating/printing
	}
}

Output:

Original Product list as per insertion-order :- 

Product [id=101, name=Wheat, quantity=1089, price=36.89]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=105, name=Vegetables, quantity=303, price=45.5]

Increasing price - Sorted Product list :- 

Product [id=101, name=Wheat, quantity=1089, price=36.89]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=104, name=Oil, quantity=208, price=164.75]

Decreasing price - Sorted Product list :- 

Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Product [id=101, name=Wheat, quantity=1089, price=36.89]

3. Before Java 8 approach and reversed() :

  • Before Java 8, we have to write/code separate Comparator for comparing on the basis of one/more attributes defined in the Object for example, Student, Employee, Person, Product, etc.
  • In this illustration, we are going to write ProductComparator for comparing its quantity for the list of Products
  • Comparator returned from this class helps to sort list of Products according to increasing order (or ascending-order) of quantity

ProductComparator.java

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

import java.util.Comparator;

public class ProductComparator implements Comparator<Product> {

	@Override
	public int compare(Product prod1, Product prod2) {
		return Long.compare(prod1.getQuantity(), prod2.getQuantity());
	}
}

3.1 To sort Product list in decreasing order of its quantity

  • A list contains Product information for 5 as per insertion-order
  • First, we are going to sort the Product list on the basis of increasing quantity
  • Second, we are going to sort the Product list on the basis of decreasing quantity
package net.bench.resources.java8.comparator.sorting;

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

public class SortingProductListWithQuantity {

	// List of Products
	private static List<Product> getProductList() {

		return Arrays.asList(
				new Product(101, "Wheat", 1089L, 36.89),
				new Product(102, "Rice", 502L, 58.19),
				new Product(103, "Lentils", 803L, 102.45),
				new Product(104, "Oil", 208L, 164.75),
				new Product(105, "Vegetables", 303L, 45.50)
				);
	}

	public static void main(String[] args) {

		// 1. get Product list
		List<Product> products = getProductList();


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



		// 2. sorting in ascending order of Product quantity
		System.out.println("\nIncreasing quantity - Sorted Product list :- \n");


		// 2.1 get comparator for sorting according to increasing quantity
		Comparator<Product> qtyComparatorAsc = 
				new ProductComparator(); // increasing quantity


		products // original data source
		.stream() // 1. get sequential stream
		.sorted(qtyComparatorAsc) // 2. sorting increasing quantity
		.forEach(System.out::println); // 3. iterating/printing



		// 3. sorting in descending order of Product quantity
		System.out.println("\nDecreasing quantity - Sorted Product list :- \n");


		// 3.1 get comparator for sorting according to decreasing quantity
		Comparator<Product> qtyComparatorDesc = 
				new ProductComparator() // increasing order
				.reversed(); // decreasing order


		products // original data source
		.stream() // 1. get sequential stream
		.sorted(qtyComparatorDesc) // 2. sorting decreasing quantity
		.forEach(System.out::println); // 3. iterating/printing
	}
}

Output:

Original Product list as per insertion-order :- 

Product [id=101, name=Wheat, quantity=1089, price=36.89]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=105, name=Vegetables, quantity=303, price=45.5]

Increasing quantity - Sorted Product list :- 

Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=101, name=Wheat, quantity=1089, price=36.89]

Decreasing quantity - Sorted Product list :- 

Product [id=101, name=Wheat, quantity=1089, price=36.89]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Product [id=104, name=Oil, quantity=208, price=164.75]

References:

Happy Coding !!
Happy Learning !!

Java 8 – Comparator.comparing() method
Java 8 - Comparator.naturalOrder() and Comparator.reverseOrder()