Sort elements of ArrayList - Java Collection Framework

Java examples for Collection Framework:ArrayList

Description

Sort elements of ArrayList

Demo Code


import java.util.ArrayList;
import java.util.Collections;
 
public class Main {
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
   /*w  ww  .  j  a v  a 2  s.c o m*/
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("3");
    arrayList.add("5");
    arrayList.add("2");
    arrayList.add("4");
 
    Collections.sort(arrayList);
   
    System.out.println("ArrayList elements after sorting in ascending order : ");
    for(int i=0; i<arrayList.size(); i++)
      System.out.println(arrayList.get(i));
  }
}

Result


Related Tutorials