1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/* Sort Java ArrayList in descending order using comparator example This java example shows how to sort elements of Java ArrayList in descending order using comparator and reverseOrder method of Collections class. */ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class SortArrayListInDescendingOrderExample { public static void main(String[] args) { //create an ArrayList object ArrayList arrayList = new ArrayList(); //Add elements to Arraylist arrayList.add("A"); arrayList.add("B"); arrayList.add("C"); arrayList.add("D"); arrayList.add("E"); /* To get comparator that imposes reverse order on a Collection use static Comparator reverseOrder() method of Collections class */ Comparator comparator = Collections.reverseOrder(); System.out.println("Before sorting ArrayList in descending order : " + arrayList); /* To sort an ArrayList using comparator use, static void sort(List list, Comparator c) method of Collections class. */ Collections.sort(arrayList,comparator); System.out.println("After sorting ArrayList in descending order : " + arrayList); } } /* Output would be Before sorting ArrayList in descending order : [A, B, C, D, E] After sorting ArrayList in descending order : [E, D, C, B, A] */ |
Add Comment