简体   繁体   English

如何对由字符串数组组成的数组列表进行排序?

[英]How to sort an Array List made of String arrays?

I have a very problematic problem! 我有一个非常有问题的问题! I have an array list that looks like this: 我有一个看起来像这样的数组列表:

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

In each string array that is added to the ArrayList looks like this: 在添加到ArrayList的每个字符串数组中,如下所示:

 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"};
 ...........

How do I organize them and store them in the ArrayList? 如何组织它们并将它们存储在ArrayList中? I am sorry to mention this, but my objective requires me to organize them in ArrayList form. 对此我很抱歉,但是我的目标是要求我以ArrayList形式组织它们。 I can't (i'm not allowed to) create a separate class for the data.... 我不能(不允许)为数据创建一个单独的类。

You can do it like this: 您可以这样做:

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);
    }
});

This was my original post but I edited this to make it more simple: 这是我的原始帖子,但我对其进行了编辑以使其更简单:

Here is the way to sort the ArrayList<String[]> : 这是对ArrayList<String[]>进行排序的方法:

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));
    }
}

Output: 输出:

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

You should write a Comparator to compare the Strings the way you want to. 您应该编写一个Comparator来比较您想要的字符串。 Then sort your ArrayList using the implemented comparator. 然后使用实现的比较器对ArrayList进行排序。 See: http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html 参见: http : //docs.oracle.com/javase/6/docs/api/java/util/Comparator.html

You can implement this by the following way. 您可以通过以下方式实现此目的。 It is advisable to use List interface instead of ArrayList object. 建议使用List接口而不是ArrayList对象。

public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<String> entries = new ArrayList<String>();
        entries.add("0 - name1");
        entries.add("1000 - name2");
        entries.add("1004 - name4");
        entries.add("1002 - name3");
        entries.add("10000 - name5");
        entries.add("2000 - name5");

        Comparator<String> comparator = new MyComparator();
        Collections.sort(entries, comparator );

        for (String e : entries){
            System.out.println(e);
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM