Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: alphabetizing , plz help

  1. #1
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default alphabetizing , plz help

    i'm trying to add a little order to my list box by alphabetizing the array list it gets the string from
    the only problem is i keep running in to dead ends ><

    can some 1 plz help

    here's the class that sits in the array list
    class Item {
    	String name;
    	int count;
    	public Item (String name,int count){
    		this.name= name;
    		this.count= count;
    	}
    }

    all i need is a sort method for an array list that will hold lots of them
    Programming: the art that fights back


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: alphabetizing , plz help

    So lets say all items in the list are strings, this is what we do.

            final List<String> letters = Arrays.asList(new String[] { "f", "g", "e", "o", "a", "y", "r" });
            Collections.sort(letters);
     
            for (final String letter : letters) {
                System.out.println(letter);
            }

    That will sort your list using natural order and since String implements Comparable this should work just fine

    Enjoy the buckets!

    // Json

  3. #3
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: alphabetizing , plz help

    useing that , would i be able to change what items go where in the list while the string being read is inside of the items?
    cause really thats what i need
    Programming: the art that fights back

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: alphabetizing , plz help

    You could create your own comparator and do it.

    What are your sorting rules?

    // Json

  5. #5
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: alphabetizing , plz help

    ok , u lost me there ><

    idk what u mean by sorting rules , i mean isnt alphabetizing only done in 1 way anyways ?
    Programming: the art that fights back

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: alphabetizing , plz help

    Well, using Collections.sort passing in a list of Strings it will automatically sort them alphabetically because thats how strings are sorted naturally just like ints would be 0,1,2,3,4 etc.

    What I'm wondering is what more you wish to do when sorting this.

    // Json

  7. #7
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: alphabetizing , plz help

    how it sorts would be perfect
    the only problem is with the fact my strings are in a object in the list
    ( to get 1 of the strings i use something like list.get(1).string )
    Programming: the art that fights back

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: alphabetizing , plz help

    Implement comparable (or comparator, I forget which one Collections.sort uses), then simply call the comparable portion of the string class:

    class Item implements Comparable<Item>
    {
    // Comparable example, I believe Comparator is very similar
    // your Item code is above, I was just too lazy to copy/paste
     
         // CompareTo method from Comparable
         public int compareTo(Item other)
         {
              return this.name.compareTo(other.name);
         }
    }

  9. #9
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: alphabetizing , plz help

    not sure how that would have worked
    but i found a round about way to use the
    collections .sort();
    i copyed all the strings to an array then sorted that ,
    after that i checked where the sorted string was and added
    the object that contained that string to a new array witch then
    overwrote the original one
    Programming: the art that fights back

  10. #10
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: alphabetizing , plz help

    What helloworld means is that you can have your object that has the string in it implement Comparable. After that all you need to do is Collections.sort(listOfObjectsHere) and it will automatically sort using the compareTo method you implemented which in turn just calls the compareTo method on the String itself and off we go.

    // Json

  11. #11
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: alphabetizing , plz help

    ah , ok
    i'll have to try that next time
    Programming: the art that fights back