Results 1 to 6 of 6
Thread: Sorting question !!!
- 06-17-2009, 11:31 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 28
- Rep Power
- 0
Sorting question !!!
Hello,
I have a question about sorting.
How can I sort HashMap by element in the HashMap ?
That's meaning, if I have in the Sys class map such as:
and in the RaceTeam class I have this variables:PHP Code:protected Map <Constructors,RaceTeam>raceTeam=new HashMap<Constructors,RaceTeam>();
and I want to sort the the eaceTeam map by the "totalScore" from the biggest to the smallest, How can I do this thing ?PHP Code:protected Constructors name; protected int totalScore; protected URL website; protected double budget; protected Race lastRace;
I do the follwing:
The out put will be:PHP Code:TreeMap<Integer,RaceTeam> teams = new TreeMap<Constructors,RaceTeam> teams = new TreeMap<Constructors,RaceTeam>(); Iterator iter = teams.keySet().iterator(); Object obj; while (iter.hasNext()) { obj = iter.next(); System.out.println(teams.get(obj).getName() + " " + teams.get(obj).getTotalScore()); }
Brawn 390
Toyota 584
BMW_Sauber 470
Renault 782
Toro_Rosso 488
Force_India 0
Ferrari 1049
I want it to be:
Ferrari 1049
Renault 782
Toyota 584
Toro_Rosso 488
BMW_Sauber 470
Brawn 390
Force_India 0Last edited by alrebatsd; 06-17-2009 at 11:36 PM.
- 06-18-2009, 01:19 AM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Design your own comparator or make your RaceTeam class implement comparable
e.g.
Java Code:public class RaceTeam implements Comparable<RaceTeam>{ public int compareTo(RaceTeam anotherTeam){ return ((getTotalScore()-anotherTeam.getTotalScore())*(-1)); }//this should do what you want... }//below is a more readable method... also this one could be faultycompareTo methods in Comparator<T>/Comparable<T> return -x if the object the method is being called on is 'less than' the parameter, 0 if the parameter is 'equal to' the object, and x if the object is 'greater than' the parameter.Java Code://more reliable public class RaceTeam implements Comparable<RaceTeam>{ public int compareTo(RaceTeam anotherTeam){ if(getTotalScore()==anotherTeam.getTotalScore()) return 0; else if(getTotalScore()>anotherTeam.getTotalScore()) return 1; else return 0; }//this will do what you want }//probably more readable than above, and has no possibility of error
Simply, negative for less than, 0 for equals, and positive for greater than.
Hopefully this helps,
Singing Boyo
EDIT: You probably don't want to sort whatever Map you are using. Instead, transfer whatever needs sorting into an ArrayList or some other storage facility with a sort method, and sort that. Maps are just that... maps. They shouldn't be ordered if it is at all avoidable.Last edited by Singing Boyo; 06-18-2009 at 01:24 AM.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
-
Unless you use a sortable Map such as a TreeMap; then it's a Map that... sorts!
- 06-18-2009, 08:09 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 28
- Rep Power
- 0
Hello'
Thank you, but In the HomwWork I cannot use the Collection sort.
in another side I do the compareTo, but I didn't know how can I use the compare to for comparing !!
Should i iterate on the map ?
Anyone can giving me an example how to use the the compareTo() ?
thank you :)
- 06-18-2009, 09:56 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 28
- Rep Power
- 0
Hello,
I do the method like this:
The out put is:PHP Code:public void printRaceTeamsRankedReport(){ TreeMap<Constructors,RaceTeam> teams = new TreeMap<Constructors,RaceTeam>(); teams.putAll( raceTeam); TreeMap <Integer,Constructors> teamCo = new TreeMap <Integer,Constructors>(); Object obj; Iterator itera = teams.keySet().iterator(); while ((itera.hasNext())) { obj = itera.next(); teamCo.put(teams.get(obj).getTotalScore(), teams.get(obj).getName()); } MyFileLogWriter.writeToFileInSeparateLine("\n====Race Teams Ranked Report===="); Iterator it = teams.keySet().iterator(); // Object obj; MyFileLogWriter.writeToFileInSeparateLine(teamCo.descendingMap()); MyFileLogWriter.writeToFileInSeparateLine("====End of Race Teams Ranked Report====\n"); }
{1049=Ferrari, 782=Renault, 584=Toyota, 488=Toro_Rosso, 470=BMW_Sauber, 390=Brawn, 0=Force_India}
How can get it like this :
Ferrari 1049
Renault 782
Toyota 584
Toro_Rosso 488
BMW_Sauber 470
Brawn 390
Force_India 0
- 06-18-2009, 11:02 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
trouble with sorting! T^T
By PureAwesomeness in forum New To JavaReplies: 1Last Post: 03-09-2009, 12:44 PM -
sorting
By jot321 in forum New To JavaReplies: 18Last Post: 10-02-2008, 10:30 AM -
Sorting CachedRowset
By Sayed in forum Advanced JavaReplies: 0Last Post: 07-18-2008, 12:14 PM -
Quick question about sorting
By nbd223 in forum New To JavaReplies: 10Last Post: 04-28-2008, 09:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks