I am doing a game, where a user enters his name after playing the game. This name together with the percentage score are being written (stored) in a textfile called players.txt I am however trying to sort the percentage score with the highest placed on top. I have written this piece of code, but i'm stuck since altough the names and scores are being written on the file, they are not being sorted. Pls can someone help me sort them? Thanks a lot.
Code:
String[] names = { JOptionPane.showInputDialog(null, "Enter your name")};
int[] scores = {percent};
BufferedWriter out = null;
try {
out = new BufferedWriter(
new FileWriter("players.txt", true));
int temp, counter, index;
for(int j = 0; j < names.length; j++) {
for (int i=0; i<n-1; i++) {
for ( j=0; j<n-1-i; j++)
if (scores[j+1] < scores[j]) {
int tmp = scores[j];
scores[j] = scores[j+1];
scores[j+1] = tmp;
}
}
out.write(names[j]);
out.write(" ");
out.write(String.valueOf(scores[j]));
out.newLine();
}
} catch(IOException e) {
System.out.println("write error: " + e.getMessage());
} finally {
if(out != null) {
try {
out.close();
} catch(IOException e) {
System.out.println("closing out: " +
e.getMessage());
}
}
}
Comment