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.
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
Parameter
Description
cmp
(Second overload) The comparator to reverse. If null, natural ordering is reversed.
Sorted collections too. A TreeSet built with reverseOrder() keeps itself in descending order as elements arrive.
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);
}
}
Output
longest first: [banana, kiwi, plum, fig]
Z to A, any case: [c, b, A]
How It Works
Base comparator.byLength sorts short words first; reverseOrder(byLength) sorts long words first.
Ties stay put. "kiwi" and "plum" both have 4 letters; the stable sort keeps kiwi before plum, as in the input.
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));
}
}
Output
poll order: 9 7 3 1
rev.compare(2, 8) = 1
How It Works
Reversed priority. With reverseOrder(), the "smallest" element by the comparator is the largest number, so poll returns 9 first.
Poll, do not print. The loop drains the queue in priority order; printing the queue directly would show its internal heap layout instead.
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.