[英]Sorting an Array List of String[] arrays
我正在阅读.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]));
}
});
两点:
compare
的结果乘以-1来反转比较。 Integer.MIN_VALUE * -1
仍然是Integer.MIN_VALUE
。 相反,颠倒比较本身的顺序 就像是:
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;
}
});
或者,首先过滤列表以确保所有行都有足够的列。
共享代码,以防有人需要对多列进行排序。
public final class ArrayComparatorWithIndex<T extends Comparable<T>> implements Comparator<T[]>
{
private final int[] indexToSort;
public ArrayComparatorWithIndex(int[] indexToSort)
{
if(indexToSort == null || indexToSort.length == 0){
throw new IllegalArgumentException("Index to use for sorting cannot be null or empty.");
}
this.indexToSort = indexToSort;
}
@Override
public int compare(T[] str, T[] otherStr)
{
int result= 0;
for (int index : indexToSort)
{
result= str[index].compareTo(otherStr[index]);
if (result != 0){
break;
}
}
return result;
}
}
//Example how to use it:
int[] indexForSorting= new int[] { 1, 3 };
Collections.sort(stringList, new ArrayComparatorWithIndex<String>(indexForSorting));
好吧,字符串[sortNum]或otherStrings [sortNum]可能超出范围。 您需要进行一些检查以防止这种情况发生。 此外,字符串[sortNum]或otherStrings [sortNum]可以为null。 我打赌你遇到了这两件事之一。 调用堆栈表示什么?
试试这个
首先是带有构造函数的类比较器:
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]));
}
}
并在你的代码中
Collections.sort(stringList,new MyStringArrayComparator<String[]>(index));
希望对你有用
我怀疑你在引用'sortNum'变量时可能有一个闭包问题。 请参阅Jon Skeet的封闭文章以获得一些指导,即使它处理C#中的闭包,它仍然应该是相关的。 即使你没有这个问题,这也是一个很好的阅读。 :)
您可以为空“单元格”提供默认值:
public int compare(String[] strings, String[] otherStrings) {
String one, other;
one = other = ""; // default value
if (sortNum<strings.length && strings[sortNum] != null) {
one = strings[sortNum];
}
if (sortNum<otherStrings.length && otherStrings[sortNum] != null) {
other = otherStrings[sortNum];
}
return -1 * (one.compareTo(other));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.