Alphabetically arrange user objects

Ok so I have an online game and for each connection the server program creates a “user object.” I would like to be able to arrange these user objects in a list (or arraylist or vector etc…) by alphabetical order by username. I know sorting would be easy if these were just strings but the user objects encapsulate other data besides the username. I know it would be possible to traverse the list in a for loop and then compare each username character by character but it seems like this is highly inefficient.

Anyone know of an easier way?

I looked at using Collections.sort(list); but in order for this to work there has to be a “natural ordering” of my user objects. How do you specify the natural ordering?

This should help http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html

so have the objects implement comparable and then in the compareTo method for my objects call username1.compareTo(username2)?

What about a TreeMap with the usernames for keys?

Yes

There should be another version of sort in Collections that takes a Comparator, which is the same idea as Comparable, but instead of making the objects that need to be sorted implement their sorting logic, the Comparator takes two objects and compares them. It would be easy to have a Comparator of two user objects that grabs the usernames and does the standard string compare on them.

Indeed. Comparable is a very poor fit, as it only allows you to sort your objects in one way. You have to use a Comparator, so you keep the sorting details out of your user-objects, which shouldn’t know about that anyway.

thanks guys i got it working implementing comparable but at some point i’ll probably switch to using comparator so i can arrange the list in different ways, such as by score.

The first reply was about Comparator and lhkbob and me have clearly explained why not to use Comparable. I can’t image why you would be a stubborn to pick Comparable anyway.

I agree Riven you are right Comparator is better but I had already written the code before I read your replies. ryanm told me to use comparable so thats what I did. Currently i only have the need to sort them in one way. Plus it is possible to use both comparable and comparator. By implementing comparable my objects now have a natural ordering. If at some point I need to sort in a different way a comparator will still work. I don’t see whats so bad about the way I have done it.

Not trying to pick a fight but i wasn’t being stubborn. Enlighten me as to why what I have done is so bad…

I already expected a reply like this, so I started my post that the answer was in the first reply, before ryanm.

My bad I honestly thought that was a link to comparable not comparator. I think at that point I didn’t even know that comparator existed.

That being said, whats wrong with defining the natural ordering of my objects. My objects are now similar to int, string, etc. that all have a natural ordering with no need to define a comparator. All of those objects implement comparable, why can’t my objects do the same?

When you implement Comparable, you only have one compareTo method. Since you wanted to do different comparisons, they told you to use Comparators and then swap which Comparator you are using when you want to use a different kind of comparison.

You can implement Comparable. You would pretty much just have to have a switch statement (or if-else structure) within the compareTo method that uses whichever comparison type should be used.

This might wind up being a little less modular because the variable specifying which comparison to use would have to be stored statically or in some other class somewhere. You wouldn’t want to have to change the variable in every instance of your object class whenever you wanted to change the ordering.