Results 1 to 6 of 6
  1. #1
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default ArrayList Sort Question

    I have an arraylist that is populated with a bunch of words. When I put the contents of the arraylist in a TreeSet to get rid of duplicates I start to run into problems with sorting in ascending order. See my code below: (finalList is the ArrayList)

    Java Code:
                // Getting rid of duplicates
                TreeSet h = new TreeSet(finalList);
                finalList.clear();
                finalList.addAll(h);
                Collections.sort(finalList);
    The output I get does indeed get rid of the duplicates, but it also does not display in ascending order. It does sort in alphabetical order, but it also sorts by the size of the string. So:
    Java Code:
    apple
    score
    zebra
    appealed
    trampled
    wretched
    I want it to be alphabetical a-z, size does not matter.
    Last edited by kraigballa; 05-04-2012 at 12:00 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: ArrayList Sort Question

    with a bunch of words
    Define word. Do you mean String, or is this a class you have implemented yourself? I recommend you post an SSCCE demonstrating the problem, for instance:
    Java Code:
    public class Test{
            public static void main(String[] args){
    		List<String> list = new ArrayList<String>();
    		list.add("score");
    		list.add("zebra");
    		list.add("apple");
    		list.add("trampled");
    		list.add("appealed");
    		list.add("wretched");
    		Collections.sort(list);
    		System.out.println(list);
    	}
    }
    ...prints out the String's in alphabetical order (appealed, apple, score, trampled, wretched, zebra)

  3. #3
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: ArrayList Sort Question

    Sorry, yes string not words. Now put a few duplicates in your example and then use:
    Java Code:
    // Getting rid of duplicates
                TreeSet h = new TreeSet(list);
                list.clear();
                list.addAll(h);
                Collections.sort(list);
    Which will get rid of the duplicates, but the sort will be as I described above.

  4. #4
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: ArrayList Sort Question

    If you copy the code below you wil get the same output that I am getting. It doesn't make sense why it's sorting by size too.
    Java Code:
    public class tester {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		int wordCount = 0;
    		ArrayList<String> list = new ArrayList<String>();
    		ArrayList<String> finalList = new ArrayList<String>();
            list.add("score");
            list.add("zebra");
            list.add("apple");
            list.add("trampled");
            list.add("appealed");
            list.add("wretched");
            list.add("trampled");
            list.add("appealed");
            list.add("wretched");
            
            for(int i = 0; i < list.size(); i++) {
                wordCount = Collections.frequency(list, list.get(i));
                String rightAlign = String.format("%15s", list.get(i));
                finalList.add(rightAlign+"\t\t"+(wordCount));
            }
            
            TreeSet<String> h = new TreeSet<String>(finalList);
            finalList.clear();
            finalList.addAll(h);
            Collections.sort(finalList);
            System.out.println(finalList);
    
    	}
    
    }
    Output:
    [ apple 1, score 1, zebra 1, appealed 2, trampled 2, wretched 2]
    Last edited by kraigballa; 05-04-2012 at 12:33 AM.

  5. #5
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: ArrayList Sort Question

    Ah ha...I figured out what is causing the problem. If you remove line 22 and replace line 23 with the code below it will work. However, if I need the String format how do I keep line 22 without running into this problem?
    Java Code:
    finalList.add(list.get(i)+"\t\t"+(wordCount));

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: ArrayList Sort Question

    If you want to maintain a word count, don't append to the word which will affect downstream analysis - java is object oriented and appending data into Strings in this manner can be considered poor practice. One solution would be to use a Map keyed with the String and valued with the count.

Similar Threads

  1. Sort an ArrayList of an ArrayList
    By lakepacific in forum New To Java
    Replies: 4
    Last Post: 09-27-2011, 09:32 AM
  2. Sort an ArrayList
    By überfuzz in forum New To Java
    Replies: 13
    Last Post: 02-10-2011, 01:18 PM
  3. ArrayList sort
    By Dipke in forum New To Java
    Replies: 1
    Last Post: 08-23-2010, 01:01 PM
  4. how to sort an arraylist?
    By zhangster in forum New To Java
    Replies: 5
    Last Post: 03-19-2010, 08:01 AM
  5. 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

Posting Permissions

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