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?

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.

Hi, appreciate more people! Σ ♥ = ¾Learn how to award medals... and work your way up the social rankings!

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....

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.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org