Monday, May 11, 2015

Java : Sorting ArrayList

Sorting ArrayList


Ref :how-to-sort-an-arraylist-in-java

import java.util.ArrayList;
import java.util.Collections;
 
public class SortingArrayList {
 
    public static void main(String args[]){
 
       ArrayList<String> al = new ArrayList<String> ();
 
            al.add("22");
            al.add("33");
            al.add("11");
            al.add("cc");
            al.add("bb");
            al.add("aa");
 
            System.out.println("Printing List before sorting......");
             for(String value: al){
                 System.out.println(value);
             }
 
             //Sorting...
             Collections.sort(al);
 
             System.out.println("Printing List after sorting......");
             for(String value: al){
                System.out.println(value);
             }
}
}
 

Output

Printing List before sorting……
22
33
11
cc
bb
aa
Printing List after sorting……
11
22
33
aa
bb
cc
 

1 comment:

  1. Good questions. If anyone looking for some ArrayList base interview question, which is very important from Java interview point of view, can see this list, which contains questions contributed by community.

    ReplyDelete