Change Highscore list from ASC to DSC. Urgent
Hi guys. I need help with changing the sort type of this code.
I write the output in an jTextArea Field with Descending order. it looks like
Viktor = 40
Thomas=65
Emil=30
Alfred=120
so you see that the names are sorted. i want the points (score) to be sorted.
The data is saved into an properties file which looks so
#Highscore-Properties
#Mon Jan 22 22:12:48 CET 2007
Viktor=80
Dora=90
Hans=200
Bertie=1000
Sepp=400
public HighScore()
throws Exception
{
myScore.load(new FileInputStream(System.getProperty("user.dir") + File.separator +"score.properties"));
}
/* returns Sortedmap mit key = Playername & Value-Score */
public SortedMap getScoreList()
{
SortedMap score =new TreeMap();
Enumeration keys = myScore.keys();
if (keys !=null)
{
while (keys.hasMoreElements())
{
String tname = keys.nextElement().toString();
score.put(tname, myScore.getProperty(tname));
// HOW TO CHANGE THIS PIECE OF CODE?
}
return score;
}
returnnull;
}
/* If player is in properties list and score is higher than the new
* then it writes the higher score in the file.
*@param nickname = player
*@param score = Highscore
*/
publicvoid setnewScore(String nickname,int score)
throws Exception
{
if (myScore.getProperty(nickname) !=null && Integer.parseInt(myScore.getProperty(nickname).toString()) < score)
{
myScore.setProperty(nickname,""+score);
myScore.store(new FileOutputStream(System.getProperty("user.dir") + File.separator +"score.properties"),"Highscore-Properties");
}
elseif (myScore.getProperty(nickname) ==null)
{
myScore.setProperty(nickname,""+score);
myScore.store(new FileOutputStream(System.getProperty("user.dir") + File.separator +"score.properties"),"Highscore-Properties");
}
}
/*
I wrote the main method in my other visual class when the window is activated
instead of the system.out.println i wrote
jTextArea2.setText("Spieler: " + player + " Score: " + sm.get(player)+"\n"+jTextArea2.getText());
writes all players who are in the score list (alphabetical ordered)
*@param args
*/
publicstaticvoid main(String[] args)
{
try{
HighScore hs =new HighScore();
SortedMap sm = hs.getScoreList();
Iterator ene = sm.keySet().iterator();
while (ene.hasNext())
{
Object player = ene.next();
System.out.println("Player: " + player +" Score: " +sm.get(player));
}
}catch (Exception e)
{
e.printStackTrace();
}
}
}
I am sorry for my bad english.
It would be very nice if someone could tell me what to change in my code.
ty
Message was edited by:
Shadowli

