Results 1 to 6 of 6
  1. #1
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default 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:
    PHP Code:
    protected Map <Constructors,RaceTeam>raceTeam=new HashMap<Constructors,RaceTeam>();
    and in the RaceTeam class I have this variables:
    PHP Code:
    	protected Constructors name;
    	protected int totalScore;
    	protected URL website;
    	protected double budget;
    	protected Race lastRace;
    and I want to sort the the eaceTeam map by the "totalScore" from the biggest to the smallest, How can I do this thing ?

    I do the follwing:
    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());
    		    }
    The out put will be:
    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 0
    Last edited by alrebatsd; 06-17-2009 at 11:36 PM.

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

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

    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!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Singing Boyo View Post
    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.
    Unless you use a sortable Map such as a TreeMap; then it's a Map that... sorts!

  4. #4
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    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 :)

  5. #5
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    Hello,

    I do the method like this:

    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");		
    		
    	}
    The out put is:
    {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

  6. #6
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    Thank you brothers,

    I do the method :)
    I did loop thru the map

    Thank u ,

Similar Threads

  1. trouble with sorting! T^T
    By PureAwesomeness in forum New To Java
    Replies: 1
    Last Post: 03-09-2009, 12:44 PM
  2. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 10:30 AM
  3. Sorting CachedRowset
    By Sayed in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2008, 12:14 PM
  4. Quick question about sorting
    By nbd223 in forum New To Java
    Replies: 10
    Last Post: 04-28-2008, 09:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •