【发布时间】:2020-06-16 16:42:42
【问题描述】:
我正在尝试在 Java 中对二维字符串数组进行排序,使用特定列作为对数组进行排序的键。问题是此列中有空值,我收到NullException 错误。有关如何处理此类问题的任何建议?以下是我到目前为止尝试过的代码,没有任何运气
public static void sortbyColumn(String arr[][], int col) {
Arrays.sort(arr, new Comparator<String[]>(){
@Override
public int compare(String[] first, String[] second){
if(first[2] == null) {
return 0;
}
// compare the first element
int comparedTo = first[2].compareTo(second[2]);
// if the first element is same (result is 0), compare the second element
if (comparedTo == 0) {
return first[2].compareTo(second[2]);
}
else {
return comparedTo;
}
}
});
}
还有一些表示数组的演示数据:
[日期、Amt、id、货币、名称、活动、标志]
[2010-02-26, 1000000, XX1, USD, Fund1, No, 0]
[2010-02-26, 1000000, XX10, USD, Fund10, No, 0]
[2010-02-26, 1000000, XX10, USD, Fund10, No, 0]
[2010-02-26, 1000000, XX9, USD, Fund9, No, 0]
[空,空,空,空,空,空,空]
【问题讨论】: