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:
protected Map <Constructors,RaceTeam>raceTeam=new HashMap<Constructors,RaceTeam>();
and in the RaceTeam class I have this variables:
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:
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