Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Alphabetically arrange user objects  (Read 2060 times)
0 Members and 1 Guest are viewing this topic.
Offline neoskunk

Junior Member





« Posted 2010-12-05 00:09:00 »

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?
Offline zoto

Senior Member


Medals: 4



« Reply #1 - Posted 2010-12-05 00:47:49 »

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

Junior Member





« Reply #2 - Posted 2010-12-05 03:30:27 »

so have the objects implement comparable and then in the compareTo method for my objects call username1.compareTo(username2)?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline fireside

Junior Member


Projects: 1



« Reply #3 - Posted 2010-12-05 06:08:12 »

What about a TreeMap with the usernames for keys?
Offline ryanm
« League of Dukes »

Senior Member


Projects: 1


Used to be bleb


« Reply #4 - Posted 2010-12-05 08:01:29 »

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

Yes
Online lhkbob

JGO Knight


Medals: 32



« Reply #5 - Posted 2010-12-05 08:07:39 »

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.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 439
Projects: 4


Hand over your head.


« Reply #6 - Posted 2010-12-05 14:19:57 »

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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline neoskunk

Junior Member





« Reply #7 - Posted 2010-12-05 22:15:48 »

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.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 439
Projects: 4


Hand over your head.


« Reply #8 - Posted 2010-12-05 22:26:44 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline neoskunk

Junior Member





« Reply #9 - Posted 2010-12-08 00:23:36 »

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....
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Online Riven
« League of Dukes »

JGO Overlord


Medals: 439
Projects: 4


Hand over your head.


« Reply #10 - Posted 2010-12-08 00:26:53 »

I agree Riven you are right Comparator is better but I had already written the code before I read your replies.

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline neoskunk

Junior Member





« Reply #11 - Posted 2010-12-08 00:40:25 »

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? 
Offline fletchergames

Senior Member





« Reply #12 - Posted 2010-12-10 01:10:24 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (143 views)
2013-05-17 21:29:12

alaslipknot (151 views)
2013-05-16 21:24:48

gouessej (182 views)
2013-05-16 00:53:38

gouessej (175 views)
2013-05-16 00:17:58

theagentd (186 views)
2013-05-15 15:01:13

theagentd (171 views)
2013-05-15 15:00:54

StreetDoggy (214 views)
2013-05-14 15:56:26

kutucuk (239 views)
2013-05-12 17:10:36

kutucuk (237 views)
2013-05-12 15:36:09

UnluckyDevil (243 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.116 seconds with 21 queries.