How to Reverse order of Comparator

In this article, we will discuss how to reverse order of java.util.Comparator using Collections class’ utility reverseOrder() method

This is used to get reverse comparator, which can be used to reverse the elements of List as against original order of comparator

 

Reversing order of Comparator

Method signature:

public static Collection reverseOrder(Comparator c);

For example,

Comparator revOrder = Collections.reverseOrder(orginialComparatorOrder);

Where,

  • revOrder = descending order
  • orginialComparatorOrder = ascending order

Customer.java

  • Customer POJO with 2 member variables of Integer and String type
  • 2-arg constructor
  • Overriding toString() method
package in.bench.resources.java.collection;

public class Customer {

	// member variables
	int customerId;
	String customerName;

	// 2-arg parameterized constructor
	public Customer(int customerId, String customerName) {
		super();
		this.customerId = customerId;
		this.customerName = customerName;
	}

	// override toString() method
	@Override
	public String toString() {
		return "Customer ["
				+ "customerId=" + customerId 
				+ ", customerName=" + customerName 
				+ "]";
	}
}

CustomerIdComparator.java

  • This is separate class which implements Comparator interface providing customized sorting logic
  • compare() method provides sorting logic, according to customer Id
package in.bench.resources.java.collection;

import java.util.Comparator;

public class CustomerIdComparator implements Comparator<Customer> {

	@Override
	public int compare(Customer o1, Customer o2) {
		return o1.customerId - o2.customerId;
	}
}

Main class

  • This class uses above customer POJO and customized sorting logic class
  • implementing comparator interface, to store objects inside ArrayList
  • sorting according to comparator (i.e.; reverse ordering of customer Id)
  • prints customer objects in descending order of customer Id

ReverseOrderOfComparator.java

package in.bench.resources.java.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ReverseOrderOfComparator {

	public static void main(String[] args) {

		// creating ArrayList object of type Customer
		ArrayList<Customer> al = new ArrayList<Customer>();

		// adding elements to TreeSet object
		al.add(new Customer(101, "Narayan Murthy"));
		al.add(new Customer(107, "Dinesh"));
		al.add(new Customer(103, "Nandan Nilekeni"));
		al.add(new Customer(102, "Ashok Arora"));
		al.add(new Customer(104, "Shibulal"));
		al.add(new Customer(106, "Kris Gopalakrishnan"));
		al.add(new Customer(105, "Raghavan"));

		System.out.println("Before Sorting:"
				+ " Insertion Order\n");

		// insertion order
		for(Customer cust : al){
			System.out.println(cust.customerId 
					+ "  " 
					+ cust.customerName);
		}

		// original order of comparator
		CustomerIdComparator originalComparatorOrder = 
				new CustomerIdComparator();

		// sorting using
		// Collections.sort(al, originalComparatorOrder);
		Collections.sort(al, originalComparatorOrder);

		System.out.println("\n\nAfter Sorting"
				+ " using Original Comparator order\n");

		// reverse ordering of customer Id using Comparator
		for(Customer cust : al){
			System.out.println(cust.customerId 
					+ "  " 
					+ cust.customerName);
		}


		// original order of comparator
		Comparator<Customer> reverseComparatorOrder = 
				Collections.reverseOrder(originalComparatorOrder);

		// sorting using Collections.sort(al, reverseComparatorOrder);
		Collections.sort(al, reverseComparatorOrder);

		System.out.println("\n\nAfter Sorting"
				+ " using reverse Comparator order\n");

		// reverse ordering of customer Id using Comparator
		for(Customer cust : al){
			System.out.println(cust.customerId 
					+ "  " 
					+ cust.customerName);
		}
	}
}

Output:

Before Sorting: Insertion Order

101  Narayan Murthy
107  Dinesh
103  Nandan Nilekeni
102  Ashok Arora
104  Shibulal
106  Kris Gopalakrishnan
105  Raghavan


After Sorting using Original Comparator order

101  Narayan Murthy
102  Ashok Arora
103  Nandan Nilekeni
104  Shibulal
105  Raghavan
106  Kris Gopalakrishnan
107  Dinesh


After Sorting using reverse Comparator order

107  Dinesh
106  Kris Gopalakrishnan
105  Raghavan
104  Shibulal
103  Nandan Nilekeni
102  Ashok Arora
101  Narayan Murthy

 

Difference between reverse() and reverseOrder() method ?

  • reverse() method is used to reverse the order of elements present in List
  • reverseOrder() method is used to reverse the order of Comparator (as seen in the above example)

 

References:

 

Happy Coding !!
Happy Learning !!

How to count duplicate elements of ArrayList
How to Reverse order of elements in ArrayList