Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对包含字符串数组的arraylist进行排序?_Java_Sorting_Arraylist - Fatal编程技术网

Java 如何对包含字符串数组的arraylist进行排序?

Java 如何对包含字符串数组的arraylist进行排序?,java,sorting,arraylist,Java,Sorting,Arraylist,不起作用,因为它用于排序 Collection.sort(); 列表 而不是 List<String> 列表 为了清楚起见,我不想对单个字符串[]元素进行排序。我想根据字符串数组的第一个元素对整个列表进行排序。检查以下内容: List<String[]> allWordList = new ArrayList<>(); List<String[]> 来源于您可以提供自己的比较器,例如作为lambda表达式: Collections.so

不起作用,因为它用于排序

Collection.sort();
列表
而不是

List<String>
列表
为了清楚起见,我不想对单个字符串[]元素进行排序。我想根据字符串数组的第一个元素对整个列表进行排序。

检查以下内容:

List<String[]> allWordList = new ArrayList<>();
List<String[]>

来源于

您可以提供自己的比较器,例如作为lambda表达式:

Collections.sort(teamsName.subList(1, teamsName.size()));

一个简单的自定义比较器应该可以做到这一点

唯一棘手的事情是确保没有索引到空数组中:

allWordList.sort((o1, o2) -> o1[0].compareTo(o2[0]));
Collections.sort(allWordList,newcomparator(){
公共整数比较(字符串[]o1,字符串[]o2){
如果(o1.length==0){
返回o2.length==0?0:-1;
}
如果(o2.length==0){
返回1;
}
返回o2[0]。比较(o1[0]);
}
});

我会使用@dasblinkenlight答案的变体,我认为比较数组长度不是正确的行为

Collections.sort(allWordList, new Comparator<String[]>() {
    public int compare(String[] o1, String[] o2) {
        if (o1.length == 0) {
            return o2.length == 0 ? 0 : -1;
        }
        if (o2.length == 0) {
            return 1;
        }
        return o2[0].compareTo(o1[0]);
    }
});
private void sort(){
List allWordList=new ArrayList();
//填写清单。。。
sort(allWordList,newfirstElementComparator());
//等等。。。
}
私有静态类FirstElementComparator实现Comparator
{
@凌驾
公共整数比较(字符串[]sa1,字符串[]sa2){
字符串str1=sa1[0];
字符串str2=sa2[0];
int结果=0;
如果(str1==null){
结果=(str2==null)?0:-1;
}
否则{
结果=str1。与(str2)相比;
}
返回结果;
}
}

您不需要编写自己的:

如果排序应区分大小写,则可以省略


或者,如果您需要依赖于区域设置的排序,请查看。

可能重复的@Arrthur:Not a duplicate我在列表中有string[],而另一个问题只有string您知道OP有一个字符串数组吗?不起作用说第一个参数错误>>找到:list,必需:string[][@vivekverma您是对的,这应该是
Collections.sort
。非常感谢。为什么要检查数组大小并允许空数组?数组的大小应为2。“通过比较字符串数组的第一个元素对该列表进行排序”。在没有进一步说明的情况下,数组为
null
或空是一个错误,与第一个元素为
null
是一个错误相同。
null
和empty都表示“丢失”,应该失败!如果您想为缺少的元素(空数组)发明排序规则,您也可以为null发明排序规则。由于值是字符串数组,可能
s1
(用于“字符串”)或
a1
(用于“数组”)比
o1
(用于“对象”)更好。
Collections.sort(allWordList, new Comparator<String[]>() {
    public int compare(String[] o1, String[] o2) {
        if (o1.length == 0) {
            return o2.length == 0 ? 0 : -1;
        }
        if (o2.length == 0) {
            return 1;
        }
        return o2[0].compareTo(o1[0]);
    }
});
private void sort() {
    List<String[]> allWordList = new ArrayList<String[]>();
    // fill the list...
    Collections.sort(allWordList, new FirstElementComparator());
    // and so on...
}

private static class FirstElementComparator implements Comparator<String[]>
{
    @Override
    public int compare(String[] sa1, String[] sa2) {
        String str1 = sa1[0];
        String str2 = sa2[0];

        int result = 0;
        if (str1 == null) {
            result = (str2 == null) ? 0 : -1;
        }
        else {
            result = str1.compareTo(str2);
        }
        return result;
    }
}
allWordList
    .stream()
    .sorted(Comparator.comparing(a -> a[0], String.CASE_INSENSITIVE_ORDER))
    .collect(Collectors.toList());