Re: alphabetizing , plz help
So lets say all items in the list are strings, this is what we do.
Code :
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
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
Re: alphabetizing , plz help
You could create your own comparator and do it.
What are your sorting rules?
// Json
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 ?
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
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 )
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:
Code :
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);
}
}
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
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
Re: alphabetizing , plz help
ah , ok
i'll have to try that next time