Ive got a file called HighScores.txt which contains player names and number of points:
name1 2
name2 5
name3 1
name4 23
name5 51
And heres my code that reads the contents of this file, splitting each line and appending it to the ArrayList highScores
:
public class fileHandling {
static ArrayList highScores = new ArrayList();
public static void readFile(String[] args) throws Exception {
File file = new File(fileHandling.class.getClassLoader()
.getResource("HighScores.txt").getPath());
Scanner read = new Scanner(file);
while (read.hasNextLine()) {
String line = read.nextLine();
String[] result = line.split("\\s+");
highScores.add(Arrays.toString(result));
System.out.println(highScores);
}
}
}
How do i then sort this ArrayList by the number of points each player has?
i.e. so the new array list will be:
[[name5, 51], [name4, 23], [name2, 5], [name1, 2], [name3, 1]]