package com.candidjava; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; public class ArrayListAscending { public static void main(String[] args) { ArrayList al = new ArrayList(); al.add("reena"); al.add("teena"); al.add("anu"); al.add("zahir"); al.add("sonu"); System.out.println("Unsorted ArrayList:"); System.out.println(al); // Sorting Arraylist in ascending order System.out.println("Arraylist in Ascending order:"); Collections.sort(al); Iterator itr = al.iterator(); while (itr.hasNext()) { String name = (String) itr.next(); System.out.println(name); } } }
OUTPUT
Unsorted ArrayList: [reena, teena, anu, zahir, sonu] Arraylist in Ascending order: anu reena sonu teena zahir