Java 对字符串[]数组的数组列表进行排序

Java 对字符串[]数组的数组列表进行排序,java,sorting,arraylist,comparator,Java,Sorting,Arraylist,Comparator,我在一个.csv文件中阅读,有点像excel中的电子表格。有一定数量的列,由文件决定,我使用.split(“,”方法将每一行读入字符串数组。然后我将其放入一个数组列表中,这样它就可以保存所有的字符串数组,而无需指定具体的大小。但是,当我使用Collections.sort()对数组列表进行排序时,程序会中断。有什么问题吗?以下是我要排序的代码: Collections.sort(stringList, new Comparator < String[] > () { publ

我在一个
.csv
文件中阅读,有点像excel中的电子表格。有一定数量的列,由文件决定,我使用
.split(“,”
方法将每一行读入字符串数组。然后我将其放入一个数组列表中,这样它就可以保存所有的字符串数组,而无需指定具体的大小。但是,当我使用
Collections.sort()
对数组列表进行排序时,程序会中断。有什么问题吗?以下是我要排序的代码:

Collections.sort(stringList, new Comparator < String[] > () {
    public int compare(String[] strings, String[] otherStrings) {
        return -1 * (strings[sortNum].compareTo(otherStrings[sortNum]));
    }
});
Collections.sort(stringList,新的比较器(){
公共整数比较(字符串[]字符串,字符串[]其他字符串){
返回-1*(字符串[sortNum].compareTo(其他字符串[sortNum]);
}
});

嗯,字符串[sortNum]或其他字符串[sortNum]都可能超出范围。你需要做一些检查来防止这种情况。此外,字符串[sortNum]或其他字符串[sortNum]可以为空。我打赌你会遇到这两件事中的一件。调用堆栈指示什么?

两点:

  • 不要将
    compare
    的结果乘以-1来反转比较
    Integer.MIN_值*-1
    仍然是
    Integer.MIN_值
    。相反,要颠倒比较本身的顺序
  • 我的猜测是,实际上有些行没有足够的列。也许你应该把这些放在最后
比如:

Collections.sort(stringList, new Comparator < String[] > () {
    public int compare(String[] x1, String[] x2) {
        if (x1.length > sortNum && x2.length > sortNum) {
            return x2[sortNum].compareTo(x1[sortNum]); 
        }
        if (x1.length > sortNum) {
            return 1;
        }
        if (x2.length > sortNum) {
            return -1;
        }
        return x2.length - x1.length;
    }
});
Collections.sort(stringList,新的比较器(){
公共整数比较(字符串[]x1,字符串[]x2){
if(x1.length>sortNum&&x2.length>sortNum){
返回x2[sortNum]。比较到(x1[sortNum]);
}
如果(x1.length>sortNum){
返回1;
}
如果(x2.length>sortNum){
返回-1;
}
返回x2.length-x1.length;
}
});

或者,首先筛选列表,以确保所有行都有足够的列。

我怀疑您在引用'sortNum'变量时可能存在闭包问题。请参阅以获得一些指导,即使它处理C#中的闭包,它仍然应该是相关的。即使你没有这个问题,这也是一本很好的读物。:)

您可以为空“单元格”提供默认值:

public int compare(字符串[]字符串,字符串[]其他字符串){
一串一串,;
one=other=”“;//默认值
如果(sortNum尝试使用此

首先使用构造函数创建类比较器:

public class MyStringArrayComparator implements Comparator<String[]>{

       Integer sortNum;

       public MyStringComparator(Integer index) {
              sortNum = index;
       }

       @Override
       public int compare(String[] strings, String[] otherStrings) {
              return -1*(strings[sortNum].compareTo(otherStrings[sortNum]));
       }
}
公共类MyStringArrayComparator实现Comparator{
整型sortNum;
公共MyStringComparator(整数索引){
sortNum=指数;
}
@凌驾
公共整数比较(字符串[]字符串,字符串[]其他字符串){
返回-1*(字符串[sortNum].compareTo(其他字符串[sortNum]);
}
}
在你的代码里

Collections.sort(stringList,new MyStringArrayComparator<String[]>(index));
Collections.sort(stringList,新的myStringGarrayComparator(索引));

希望这对共享代码的您有用,以防有人需要对多个列进行排序

公共最终类ArrayComparatorWithIndex实现了Comparator
{
私有最终int[]索引输出;
公共阵列比较与索引(int[]indexToSort)
{         
if(indexToSort==null | | indexToSort.length==0){
抛出新的IllegalArgumentException(“用于排序的索引不能为null或空”);
}
this.indexToSort=indexToSort;
}
@凌驾
公共整数比较(T[]str,T[]otherStr)
{ 
int结果=0;
for(int索引:indexToSort)
{
结果=str[index]。比较(其他str[index]);
如果(结果!=0){
打破
}
}
返回结果;
}
}
//示例如何使用它:
int[]indexForSorting=新的int[]{1,3};
排序(stringList,新的ArrayComparatorWithIndex(indexForSorting));

sortNum
从哪里来?你说的“中断”是什么意思?它在哪里没有中断?你得到了什么错误?你期望发生什么?每行都有
sortNum
+1个单元格吗?你确定
sortNum
不在
字符串[]的范围之外吗
?我假设sortNum是要按其进行反向排序的列号。我还假设它是一个字符串,您希望对其进行算术排序。它是如何断开的?是否有堆栈跟踪?
Collections.sort(stringList,new MyStringArrayComparator<String[]>(index));