プレイヤー名とポイント数を含む HighScores.txt というファイルがあります。
name1 2
name2 5
name3 1
name4 23
name5 51
そして、このファイルの内容を読み取り、各行を分割して 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);
}
}
}
この ArrayList を各プレイヤーのポイント数で並べ替えるにはどうすればよいですか?
つまり、新しい配列リストは次のようになります。
[[name5, 51], [name4, 23], [name2, 5], [name1, 2], [name3, 1]]