Java 需要制作前10个字符串和int';s

Java 需要制作前10个字符串和int';s,java,arrays,string,sorting,Java,Arrays,String,Sorting,我正在制作游戏插件。我需要从配置中获取10个值,并将其排序到前10位。它应该打印前10名,如: 1。姓名+积分 2.姓名+积分 . . . 10姓名+积分 使用我粘贴在下面的代码,我收到: [name1,name2,name3,name4][points1,points2,points3] 而且它不像我希望的那样排序。它应该从最高到最低[10个资源] for (String g : Rpgg.getConfig().getConfigurationSection("Stats").getKey

我正在制作游戏插件。我需要从配置中获取10个值,并将其排序到前10位。它应该打印前10名,如:

1。姓名+积分
2.姓名+积分
.
.
.
10姓名+积分
使用我粘贴在下面的代码,我收到:

[name1,name2,name3,name4][points1,points2,points3]

而且它不像我希望的那样排序。它应该从最高到最低[10个资源]

for (String g : Rpgg.getConfig().getConfigurationSection("Stats").getKeys(false)){
    // Set<String> g = Rpgg.getConfig().getConfigurationSection("Stats").getKeys(false);
    // int[] i1 = new int[]{i};
    int i = Rpgg.getConfig().getInt("Stats."+g+".pkt");
    l.add(i);

    a.add(g);
    Collections.sort(a);

    Collections.sort(l);
    map.put(g,i);
}

sorted_map.putAll(map);
Bukkit.broadcastMessage("lp:"+ "§4G:§6 " + a + "   §4pkt§6 " +  l); 
for(字符串g:Rpgg.getConfig().getConfigurationSection(“Stats”).getKeys(false)){
//Set g=Rpgg.getConfig().getConfigurationSection(“Stats”).getKeys(false);
//int[]i1=新的int[]{i};
inti=Rpgg.getConfig().getInt(“Stats.”+g+“.pkt”);
l、 加(i);
a、 添加(g);
收集.分类(a);
集合。排序(l);
map.put(g,i);
}
已排序的地图。putAll(地图);
Bukkit.广播信息(“lp:”+“§4G:§6”+a+”§4pkt§6”+l);

您可以将二维数组与信息一起使用,而不是使用两个单个数组


“它让我从最低到最高。”

如果您当前正在从最低到最高,并且您希望它以相反的顺序运行,只需使用

  • -返回一个比较器,该比较器对实现可比较接口的对象集合施加与自然顺序相反的顺序
就你而言:

Collections.sort(a, Collections.reverseOrder());

Collections.reverseOrder()
返回一个
比较器。使用此
Collections.sort
重载方法,该方法将
比较器作为参数

public static void sort(List list, Comparator c)

编辑每个用户的评论


“好的,现在我想将dat列表更改为int和string。如何操作?”

您应该做的是创建一个具有
名称
字段和
分数
字段的类,并使其实现类似的
可比性

public class User implements Comparable<User> {
    String name;
    int score;

    public User(String name, int score){
        this.name = name;
        this.score = score;
    }

    public int compareTo(User user){
        return user.getScore() - this.getScore();
    }

    public String getName(){
        return name;
    }

    public int getScore(){
        return score;
    }

    public String toString(){
        return "Name: " + name + " Score: " + score;
    }
}
toString
方法将以字符串形式给出结果。只需打印
用户
对象即可


试运行

import java.util.Collections;
import java.util.List;

public class UserDemo {

    public static void main(String[] args) {
        List<User> users = new ArrayList<User>();
        users.add(new User("Jim", 4398));
        users.add(new User("James", 9880));
        users.add(new User("Jane", 10238));
        users.add(new User("Jack", 5498));
        users.add(new User("Josh", 6530));
        users.add(new User("John", 12349));
        users.add(new User("Jim", 9438));
        users.add(new User("Jill", 12307));
        users.add(new User("Jimbo", 23454));
        users.add(new User("Jason", 5430));

        Collections.sort(users);

        for (User user : users) {
            System.out.println(user);
        }
    }
}

它现在是从低到高吗?它从低到高。好的,现在我想把dat列表改成int和string。如何做到这一点?您应该创建一个包含名称字段和分数字段的类,并使其实现可比性。请参阅我的编辑以了解我所说的内容。
import java.util.Collections;
import java.util.List;

public class UserDemo {

    public static void main(String[] args) {
        List<User> users = new ArrayList<User>();
        users.add(new User("Jim", 4398));
        users.add(new User("James", 9880));
        users.add(new User("Jane", 10238));
        users.add(new User("Jack", 5498));
        users.add(new User("Josh", 6530));
        users.add(new User("John", 12349));
        users.add(new User("Jim", 9438));
        users.add(new User("Jill", 12307));
        users.add(new User("Jimbo", 23454));
        users.add(new User("Jason", 5430));

        Collections.sort(users);

        for (User user : users) {
            System.out.println(user);
        }
    }
}
Name: Jimbo Score: 23454
Name: John Score: 12349
Name: Jill Score: 12307
Name: Jane Score: 10238
Name: James Score: 9880
Name: Jim Score: 9438
Name: Josh Score: 6530
Name: Jack Score: 5498
Name: Jason Score: 5430
Name: Jim Score: 4398