如何排序做出字符串数组的数组列表? [英] How to sort an Array List made of String arrays?

查看:180
本文介绍了如何排序做出字符串数组的数组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很成问题的问题!我有一个看起来像这样的数组列表:

I have a very problematic problem! I have an array list that looks like this:

 ArrayList<String[]> teamsToOrder = new ArrayList<String[]>();

在被添加到ArrayList每个字符串数组看起来像这样:

In each string array that is added to the ArrayList looks like this:

 String[] arrayExample = {teamNumber,score};

 String[] array1 = {"340","100"};
 String[] array2 = {"7680","70"};
 String[] array3 = {"770","110"};
 ...........

我希望能够做的是得分组织阵列(最好成绩最差),像这样:

What I want to be able to do is organize the arrays by score (best score to worst) like so:

 String[] array3 = {"770","110"};
 String[] array1 = {"340","100"};
 String[] array2 = {"7680","70"};
 ...........

如何组织并将其存储在ArrayList中?对不起,我要提到这一点,但我的目标要求我组织他们在ArrayList的形式。 我不能(我不允许)创建数据一个单独的类....

推荐答案

这是我原来的职位但我编辑这使其更简单:

This was my original post but I edited this to make it more simple:

下面是排序的的ArrayList&LT的方式;的String []&GT;

Collections.sort(teamsToOrder,new Comparator<String[]>() {
    public int compare(String[] s1, String[] s2) {

        return Integer.parseInt(s2[1]) - Integer.parseInt(s1[1]);
    }
});


就是这样。所以,作为一个例子:


That's it. So, as an example:

public static void main(String args[]) {

    ArrayList<String[]> teamsToOrder = new ArrayList<String[]>();
    String[] array1 = {"340","100"};
    String[] array2 = {"7680","70"};
    String[] array3 = {"770","110"};

    teamsToOrder.add(array1);teamsToOrder.add(array2);teamsToOrder.add(array3);

    Collections.sort(teamsToOrder,new Comparator<String[]>() {
        public int compare(String[] s1, String[] s2) {

            return Integer.parseInt(s2[1]) - Integer.parseInt(s1[1]);
        }
    });

    // display the new sorted ArrayList of String arrays:
    for (String[] s: teamsToOrder) {
        System.out.println(Arrays.toString(s));
    }
}

输出:

[770, 110]
[340, 100]
[7680, 70]

这篇关于如何排序做出字符串数组的数组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆