Sort an ArrayList in descending order in Java

We shared an article about sorting an ArrayList in ascending and descending order. In this tutorial we will see how to sort an ArrayList in descending order using the method reverseOrder().

The method Collections.reverseOrder() after calling the Collections.sort() in order to reverse the list. It can be given in two ways:

1- We sort the list with the sort() method, then we use the reverse():

Collections.sort(); 
Collections.reverse();
and the second, the call to the reveseOrder()  is done in the sort():

Collections.sort(arraylist, Collections.reverseOrder()); 
Example:

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

public class sort_reverseorder_arraylist {

public static void main(String[] args) {
ArrayList< String> arraylist = new ArrayList< String> ();

//Add Items to ArrayList
arraylist.add("ac");
arraylist.add("ab");
arraylist.add("bb");
arraylist.add("aa");
arraylist.add("ae");
arraylist.add("ba");
System.out.println("ArrayList before sorting: "+arraylist);

Collections.sort(arraylist);

System.out.println("ArrayList after sorting: "+arraylist);

//reverse elements of ArrayList
Collections.reverse(arraylist);
System.out.println("ArrayList after inversion: "+arraylist);
}
}

Runtime:

ArrayList before sorting: [ac, ab, bb, aa, ae, ba]
ArrayList after sorting: [aa, ab, ac, ae, ba, bb]
ArrayList after inversion: [bb, ba, ae, ac, ab, aa]
Note: In this example we used a String type (ArrayList), but you can use a numeric type using the same method.