Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java按最大数字对文本文件进行排序_Java_Sorting - Fatal编程技术网

如何使用Java按最大数字对文本文件进行排序

如何使用Java按最大数字对文本文件进行排序,java,sorting,Java,Sorting,我目前有一个小问题,我正在用Java制作一个游戏,我正在为此获得高分。我让它保存球员的名字和分数到一个文本文件。当我试着把它打印出来时,它显然是按照文本文件的顺序显示的,我该如何排序,以便它首先使用最高的分数 文本文件中的示例: John: 12 Bert: 16 Oscar: 18 Eric: 25 Carl: 9 我多么想要它: Eric: 25 Oscar: 18 Bert: 16 John: 12 Carl: 9 尝试使用sortCollections.sort(列表)使用排序的集合

我目前有一个小问题,我正在用Java制作一个游戏,我正在为此获得高分。我让它保存球员的名字和分数到一个文本文件。当我试着把它打印出来时,它显然是按照文本文件的顺序显示的,我该如何排序,以便它首先使用最高的分数

文本文件中的示例:

John: 12
Bert: 16
Oscar: 18
Eric: 25
Carl: 9
我多么想要它:

Eric: 25
Oscar: 18
Bert: 16
John: 12
Carl: 9

尝试使用sort
Collections.sort(列表)

使用排序的集合,如
TreeMap
,它将其条目(键值映射)按其键的自然顺序保存。因为,您希望根据高分进行排序,将您的分数作为关键,并将球员姓名作为其值

// instantiate your sorted collection
Map<Integer, String> highestScores = new TreeMap<Integer, String>();

// setup a file reader
BufferedReader reader = new BufferedReader(
                        new FileReader(new File("/path/to/file")));

String line = null;
while ((line = reader.readLine()) != null) { // read your file line by line
    String[] playerScores = line.split(": ");
    // populate your collection with score-player mappings
    highestScores.put(Integer.valueOf(playerScores[1]), playerScores[0]);
}

// iterate in descending order
for (Integer score : highestScores.descendingKeySet()) {
    System.out.println(highestScores.get(score) + ": " + score);
}
编辑:
很有可能两个或两个以上的球员会有相同的高分。因此,排序后的集合必须稍微复杂一点,但是如果您理解了上面的集合,那么理解这个集合就不会有问题

现在,我们不需要将分数映射到某个玩家,而需要将其映射到一个玩家列表(具有相同的高分):


你可以使用什么代码在文件上打印?最好是按顺序打印,而不是打印和重新排序。(除非您无法更改打印代码…)显示您的代码,以便我们可以帮助您。
Eric: 25
Oscar: 18
Bert: 16
John: 12
Carl: 9
// {key - value} = {high score - {list, of, players}}
TreeMap<Integer, List<String>> highestScores =
                               new TreeMap<Integer, List<String>>();

BufferedReader reader = new BufferedReader(
                        new FileReader(new File("/path/to/file")));

String line = null;
while ((line = reader.readLine()) != null) {
    String[] playerScores = line.split(": ");
    Integer score = Integer.valueOf(playerScores[1]);
    List<String> playerList = null;

    // check if a player with this score already exists
    if ((playerList = highestScores.get(score)) == null) { // if NOT,
        playerList = new ArrayList<String>(1); // CREATE a new list
        playerList.add(playerScores[0]);
        highestScores.put(Integer.valueOf(playerScores[1]), playerList);
    } else { // if YES, ADD to the existing list
        playerList.add(playerScores[0]);
    }
}

// iterate in descending order
for (Integer score : highestScores.descendingKeySet()) {
    for (String player : highestScores.get(score)) { // iterate over player list
        System.out.println(player + ": " + score);
    }
}
Eric: 25
Oscar: 18
Bert: 16
John: 12 *
Jane: 12 *
Carl: 9