Java Collections reverseOrder() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

In Java, Collections.reverseOrder() returns a Comparator that orders elements the opposite way to their natural ordering: largest number first, Z before A. The overload reverseOrder(cmp) flips any comparator you give it.

Pass the result anywhere a comparator is accepted: Collections.sort, TreeSet, TreeMap, or PriorityQueue.

A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.reverseOrder(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Use the no-argument form for natural order, or wrap another comparator.

Java
Collections.sort(list, Collections.reverseOrder());          // descending
Comparator<String> longFirst =
    Collections.reverseOrder(Comparator.comparingInt(String::length));
CallWhat it does
Collections.reverseOrder()Reverse of natural (Comparable) ordering.
Collections.reverseOrder(cmp)Reverse of cmp.
Collections.reverseOrder(null)Same as reverseOrder().
Comparator.reverseOrder()Java 8+ equivalent of the no-argument form.

reverseOrder() exists since Java 1.2 and reverseOrder(Comparator) since Java 1.5. Java 8 added Comparator.reverseOrder() and cmp.reversed() as alternatives.

Library declarations
Java
// java.util.Collections
public static <T> Comparator<T> reverseOrder()

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

Parameters

ParameterDescription
cmp(Second overload) The comparator to reverse. If null, natural ordering is reversed.

Return Value

Return ValueDescription
Comparator<T>A comparator that imposes the reverse order.

Compare with Similar Methods

Reversing, sorting, and picking extremes:

MethodHow it differs
reverse(list)Flips the current order of a list; does not sort.
sort(list, cmp)Sorts a list; combine with reverseOrder() for descending.
max(c)Largest element without sorting.

Java Examples

Example 1 — Sort a List and a TreeSet in Descending Order

Pass reverseOrder() to Collections.sort and to a TreeSet constructor.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class CollectionsReverseOrderDesc {
    public static void main(String[] args) {
        List<Integer> scores = new ArrayList<>(Arrays.asList(5, 1, 4, 3));
        Collections.sort(scores, Collections.reverseOrder());
        System.out.println("descending list: " + scores);

        Set<String> names = new TreeSet<String>(Collections.<String>reverseOrder());
        names.addAll(Arrays.asList("bo", "ana", "cy"));
        System.out.println("descending set:  " + names);
    }
}

How It Works

  1. Flip natural order. Integers normally sort smallest first; the reversed comparator puts 5 first.
  2. Sorted collections too. A TreeSet built with reverseOrder() keeps itself in descending order as elements arrive.
  3. Strings. Alphabetical order reversed gives cy, bo, ana.

Example 2 — Longest Word First

Reverse a length comparator, and notice how words of equal length keep their original order.

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

public class CollectionsReverseOrderLength {
    public static void main(String[] args) {
        List<String> words = new ArrayList<>(Arrays.asList("fig", "banana", "kiwi", "plum"));
        Comparator<String> byLength = Comparator.comparingInt(String::length);

        Collections.sort(words, Collections.reverseOrder(byLength));
        System.out.println("longest first: " + words);

        List<String> mixed = new ArrayList<>(Arrays.asList("b", "A", "c"));
        Collections.sort(mixed, Collections.reverseOrder(String.CASE_INSENSITIVE_ORDER));
        System.out.println("Z to A, any case: " + mixed);
    }
}

How It Works

  1. Base comparator. byLength sorts short words first; reverseOrder(byLength) sorts long words first.
  2. Ties stay put. "kiwi" and "plum" both have 4 letters; the stable sort keeps kiwi before plum, as in the input.
  3. Any comparator. Reversing CASE_INSENSITIVE_ORDER gives c, b, A, ignoring that "A" is uppercase.

Example 3 — A PriorityQueue That Returns the Largest First

A PriorityQueue normally polls the smallest element. A reversed comparator turns it into a max-first queue.

Java
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;

public class CollectionsReverseOrderHeap {
    public static void main(String[] args) {
        PriorityQueue<Integer> maxFirst = new PriorityQueue<Integer>(Collections.<Integer>reverseOrder());
        maxFirst.addAll(Arrays.asList(3, 9, 1, 7));

        StringBuilder out = new StringBuilder();
        while (!maxFirst.isEmpty()) {
            out.append(maxFirst.poll()).append(' ');
        }
        System.out.println("poll order: " + out.toString().trim());

        Comparator<Integer> rev = Collections.reverseOrder();
        System.out.println("rev.compare(2, 8) = " + rev.compare(2, 8));
    }
}

How It Works

  1. Reversed priority. With reverseOrder(), the "smallest" element by the comparator is the largest number, so poll returns 9 first.
  2. Poll, do not print. The loop drains the queue in priority order; printing the queue directly would show its internal heap layout instead.
  3. Sign flip. Naturally 2 comes before 8 (negative result); the reversed comparator returns a positive value, 1.

Frequently Asked Questions

Collections.sort(list, Collections.reverseOrder()) for natural ordering, or Collections.sort(list, Collections.reverseOrder(cmp)) for a custom comparator.
reverseOrder() returns a Comparator used for sorting; reverse(list) simply flips the current order of a list without comparing anything.
The same ordering as reverseOrder(): the reverse of natural ordering.
new PriorityQueue<Integer>(Collections.reverseOrder()) polls the largest element first.
They behave the same. Comparator.reverseOrder() (Java 8+) reads well in comparator chains; Collections.reverseOrder() also works on older Java.

Next: rotate()

Shift every element of a list by a distance, wrapping around.

rotate() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful