Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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 2531 times)
0 Members and 1 Guest are viewing this topic.
Offline neoskunk

Junior Devvie





« Posted 2010-12-04 23: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 Devvie


Medals: 4



« Reply #1 - Posted 2010-12-04 23:47:49 »

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

Junior Devvie





« Reply #2 - Posted 2010-12-05 02: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

Senior Newbie





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

What about a TreeMap with the usernames for keys?
Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


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

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

Yes
Offline lhkbob

JGO Knight


Medals: 32



« Reply #5 - Posted 2010-12-05 07: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: 836
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2010-12-05 13: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
Offline neoskunk

Junior Devvie





« Reply #7 - Posted 2010-12-05 21: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: 836
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2010-12-05 21: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
Offline neoskunk

Junior Devvie





« Reply #9 - Posted 2010-12-07 23: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!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 836
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2010-12-07 23: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
Offline neoskunk

Junior Devvie





« Reply #11 - Posted 2010-12-07 23: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 Devvie





« Reply #12 - Posted 2010-12-10 00: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.

 

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

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
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!