Java Collections reverseOrder() Method
Example
Sort a list in reverse alphabetical order:
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Collections.sort(cars, Collections.reverseOrder());
System.out.println(cars);
}
}
Definition and Usage
The reverseOrder() method returns a Comparator (an object that decides the order of two items) that sorts items in reverse order. Strings are sorted from Z to A, and numbers from the largest to the smallest.
Use it with Collections.sort() or with the sort() method of a list.
Tip: Use the reverse() method to reverse the order of a list without sorting it.
Syntax
One of the following:
public static Comparator reverseOrder()
public static Comparator reverseOrder(Comparator c)
Parameter Values
| Parameter | Description |
|---|---|
| c | Optional. A Comparator to reverse. Without it, the method reverses the natural order (alphabetical or numerical). |
Technical Details
| Returns: | A Comparator that sorts items in reverse order. |
|---|
Related Pages
Java Collections Framework Tutorial
❮ Collections Methods