It is very simple program to sort items of an ArrayList in java. You have to just use sort() method to do this. In the following program I have create one class known as "SortArrayListEg". In the following program I have use One variable to store all user value and then I have pass this variable to sort() method for sorting.
Code:
import java.util.*;
public class SortArrayListEg
{
public static void main(String s[])
{
Collections col;
List li = sort(s);
System.out.println("\nStrings sorted List ...");
for(int a = 0; a < s.length; a++)
{
System.out.println((String)li.get(a));
}
int ints[] = {
11, 8, -38, 752, 4
};
Integer ins[] = new Integer[ints.length];
for(int a = 0; a < in.length; a++)
{
in[a] = new Integer(ints[a]);
}
li = sort(in);
System.out.println(" sorted List of integers");
for(int a = 0; a < ins.length; a++)
{
System.out.println((Integer)li.get(a));
}
}
public static List sort(Object o[])
{
ArrayList ali = new ArrayList();
for(int a = 0; a < o.length; a++)
al.add(a, o[a]);
List listi = Collections.synchronizedList(ali);
Collections.sort(listi);
return listi;
}
}
Bookmarks