Sorting ArrayList of String[]

cur*_*rge 2 java sorting arraylist

I have an arraylist of String[]:

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

I want to sort them in largest-array-size ascending order. Example:

mystuff = {["this", "is", "item", "one"], ["this", "is", "item", "two"], ["item"], ["item", "three"]}

Should become:

mystuff = {["item"], ["item", "three"], ["this", "is", "item", "one"], ["this", "is", "item", "two"]}

For arrays of equal length, the order doesn't matter.

Edit:

Java编译器版本:javac 1.6.0_20

使用@ sepp2k的代码我遇到的错误:http://pastie.org/private/ienpdtj0ft6czw6nboeva

sep*_*p2k 12

使用Collections.sortComparator用于比较的长度.

Collections.sort(mystuff, new Comparator<String[]>() {
    public int compare(String[] x, String[] y) {
        if(x.length < y.length) {
            return -1;
        } else if(x.length == y.length) {
            return 0;
        } else {
            return 1;
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

编辑:这是一个完整的类,编译和运行没有错误(并显示正确的结果):

import java.util.*;

public class Bla {                         
    public static void main(String[] args) {
        // Create list
        List<String[]> mystuff = new ArrayList<String[]>();
        mystuff.add(new String[] {"lilu", "lolo"});
        mystuff.add(new String[] {"lala"});
        mystuff.add(new String[] {"lila", "blabla", "pfirsichkuchen"});

        // Sort list
        Collections.sort(mystuff, new Comparator<String[]>() {
            public int compare(String[] x, String[] y) {
                if(x.length < y.length) {
                    return -1;
                } else if(x.length == y.length) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });

        // Output list
        for(String[] strs : mystuff) {
            System.out.println(Arrays.toString(strs));
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)