Sing S
Sorting arraylist without using Collection.sort()
1 Answer(s)      4 years and 5 months ago
Posted in : Java Beginners

Hi,

How can I sort an arraylist without using Collection.sort() nad also I am not allowed to use any other data structure in the program? Is there any algorithm by using which I can do this?

Please help ....

Advertisement
View Answers

February 8, 2011 at 5:10 PM


Java Sort list without using Collections.sort() method

import java.util.*;
class SortList 
{
    public static void main(String[] args) 
    {
        ArrayList<String> list=new ArrayList<String>();
        list.add("D");
        list.add("B");
        list.add("A");
        list.add("E");
        list.add("C");

    Object arr[] = list.toArray(); 
    Arrays.sort(arr);
    for(int i=0;i<arr.length;i++){
    System.out.println(arr[i].toString());
     }
    }
}









Related Tutorials/Questions & Answers: