Sort Java ArrayList in descending order using comparator - Java Collection Framework

Java examples for Collection Framework:ArrayList

Description

Sort Java ArrayList in descending order using comparator

Demo Code


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

public class Main {

  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();

    // Add elements to Arraylist
    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("E");

    Comparator comparator = Collections.reverseOrder();

    System.out.println("Before sorting ArrayList in descending order : " + arrayList);

    Collections.sort(arrayList, comparator);
    System.out.println("After sorting ArrayList in descending order : " + arrayList);

  }/*from  w w  w  .  ja va2 s.  c  om*/
}

Result


Related Tutorials