So what I'm trying to do right now for a school project is make a small high score system using external files, and using a TreeMap to map the name of the person to their score. However just based off of the nature of the way I'm iterating through it, the numbers are fragmented, I want to make it so that they go in descending order, but I'm not sure how. Any help would be appreciated! Thanks!
File dir = new File(System.getProperty("user.dir") + "/saves");
try {
TreeMap<String, Character> scores2 = new TreeMap<>();
for (File file : dir.listFiles()) {
InputStream in = new FileInputStream(file);
String name = file.getName().replace(".dat", "");
int content;
while((content = in.read())!=-1) {
char num = (char)content;
scores2.put(name, num);
}
}
Text top5 = new Text();
for (int i = 0; i < dir.list().length; i++) {
Map.Entry<String, Character> currentEntry = scores2.pollFirstEntry();
String name = currentEntry.getKey();
Character score = currentEntry.getValue();
top5.setText(top5.getText() + name + ": " + score + "\n");
}
newPane.setAlignment(Pos.CENTER);
newPane.add(top5, 1, 1);
Button exit = new Button("Arrière");
exit.setOnMouseClicked(mEv -> scene.setRoot(pane));
newPane.add(exit, 1, 3);
scene.setRoot(newPane);
}catch(NullPointerException | IOException e) {
e.printStackTrace();
Text txt = new Text("Il n'y a pas de scores pour montrer.");
Button returnButto = new Button("Retourner?");
GridPane errPane = new GridPane();
errPane.add(txt, 1, 1);
errPane.add(returnButto, 1, 2);
errPane.setAlignment(Pos.CENTER);
scene.setRoot(errPane);
returnButto.setOnMouseClicked(mcEv -> scene.setRoot(pane));
}