我有一个非常棘手的问题!我有一个如下所示的数组列表:
ArrayList<String[]> teamsToOrder = new ArrayList<String[]>();
在添加到 ArrayList 的每个字符串数组中如下所示:
String[] arrayExample = {teamNumber,score};
String[] array1 = {"340","100"};
String[] array2 = {"7680","70"};
String[] array3 = {"770","110"};
...........
我想做的是按分数(最好的分数到最差的分数)组织数组,如下所示:
String[] array3 = {"770","110"};
String[] array1 = {"340","100"};
String[] array2 = {"7680","70"};
...........
如何组织它们并将它们存储在 ArrayList 中?很抱歉提及这一点,但我的目标要求我以 ArrayList 形式组织它们。 我不能(不允许)为数据创建一个单独的类....
最佳答案
你可以这样做:
Collections.sort(teamsToOrder, new Comparator<String[]>() {
public int compare(String[] lhs, String[] rhs) {
// Parse the scores of the two items
int leftScore = Integer.parseInt(lhs[1]);
int rightScore = Integer.parseInt(rhs[1]);
// Compare the two scores, and return the result
return Integer.compare(leftScore, rightScore);
}
});
关于java - 如何对由字符串数组组成的数组列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20821057/