Collections.sort();and the second, the call to the reveseOrder() is done in the sort():
Collections.reverse();
Collections.sort(arraylist, Collections.reverseOrder());Example:
import java.util.ArrayList;Runtime:
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);
}
}
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 |
Please disable your ad blocker and refresh the window to use this website.