Collections.reverseOrder() method in Java

realNameHidden - Sep 15 - - Dev Community

The Collections.reverseOrder() method in Java provides a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.

There is no parameter required for this method; however, it is a common misconception that the method takes an argument. Let's clarify this by providing some more details and examples.

Method Signature

The method signature of Collections.reverseOrder() is:

public static <T> Comparator<T> reverseOrder()

Enter fullscreen mode Exit fullscreen mode

This method returns a comparator that imposes the reverse of the natural ordering.

There is also an overloaded version of the method which takes a comparator as an argument:

public static <T> Comparator<T> reverseOrder(Comparator<T> cmp)

Enter fullscreen mode Exit fullscreen mode

This version returns a comparator that imposes the reverse ordering of the specified comparator.

Example 1: Using Collections.reverseOrder() with Natural Ordering
Here’s an example of how to use Collections.reverseOrder() to sort a list of integers in descending order:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(3);
        numbers.add(1);
        numbers.add(4);
        numbers.add(1);
        numbers.add(5);

        System.out.println("Original list: " + numbers);

        // Sort the list in descending order using reverseOrder()
        Collections.sort(numbers, Collections.reverseOrder());

        System.out.println("List sorted in reverse order: " + numbers);
    }
}

Enter fullscreen mode Exit fullscreen mode

Example 2: Using Collections.reverseOrder(Comparator cmp)
Here’s an example of how to use Collections.reverseOrder(Comparator cmp) to sort a list of custom objects in reverse order based on a specific comparator:

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

class Person {
    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return name + " (" + age + ")";
    }
}

public class Main {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        people.add(new Person("Alice", 30));
        people.add(new Person("Bob", 25));
        people.add(new Person("Charlie", 35));
        people.add(new Person("David", 28));

        System.out.println("Original list: " + people);

        // Sort the list by age in ascending order
        people.sort(Comparator.comparingInt(person -> person.age));
        System.out.println("List sorted by age: " + people);

        // Sort the list by age in descending order using reverseOrder
        Collections.sort(people, Collections.reverseOrder(Comparator.comparingInt(person -> person.age)));
        System.out.println("List sorted by age in reverse order: " + people);
    }
}

Enter fullscreen mode Exit fullscreen mode

Explanation of the Code

Original List: The list of Person objects is created and populated with some sample data.

Sorting by Age in Ascending Order: The list is sorted by the age field in ascending order using Comparator.comparingInt.

Sorting by Age in Descending Order: The list is sorted by the age field in descending order using Collections.reverseOrder with the age comparator.

Output: The sorted list is printed in both ascending and descending order based on the age field.

Summary

Collections.reverseOrder() provides a comparator that sorts in reverse natural order.

Collections.reverseOrder(Comparator cmp) reverses the order defined by a given comparator.

These methods are useful for sorting collections in descending order, either by natural ordering or by a specific comparator.

These examples demonstrate the usage of both Collections.reverseOrder() and Collections.reverseOrder(Comparator cmp) to sort lists in descending order.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player