Results 1 to 6 of 6
  1. #1
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default how to sort an arraylist?

    Java Code:
    private void add(ArrayList v)
    	{
    		ArrayList arrayList = new ArrayList();
    		for(int j=0; j<v.size(); j++)
    		{
    			arrayList.add(v.get(j));
    		}
    		for(int i=0;i<ret.size();i++)
    		{
    			if(Collections.sort(arrayList) ==ret.get(i))
    			ret.add(Collections.sort(arrayList));
    		}
    	}
    that is my code... im trying to sort arrayList and im getting this error
    "'void' type not allowed here"

    please help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This code doesn't make much sense to me, sorry. What are you trying to do here? If you're trying to sort an ArrayList, you simply call Collections.sort(myList) and be done with it. Note that the items in the arraylist should implement the Comparable interface.

  3. #3
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default nvm

    lol i got it...all i did was first sort the array then use the array in the if statements

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by zhangster View Post
    lol i got it...all i did was first sort the array then use the array in the if statements
    So, does this mean that your question is solved? If so, great, and please mark the thread "Solved". If not, then please clarify what still needs fixing.

    Best of luck!

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Look into SortedSet subclasses, such as TreeSet. Note that Set requires that the elements be unique, unlike a list. You can then just do something like

    Java Code:
    SortedSet sortedSet = new TreeSet(myArrayList);

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,380
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zhangster View Post
    Java Code:
    		ArrayList arrayList = new ArrayList();
    		for(int j=0; j<v.size(); j++)
    		{
    			arrayList.add(v.get(j));
    		}
    This hurts; the ArrayList class takes a copy constructor so that loop is not needed:

    Java Code:
    ArrayList arrayList= new ArrayList(v);
    kind regards,

    Jos

Similar Threads

  1. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  2. Replies: 2
    Last Post: 02-02-2009, 11:29 AM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •