【发布时间】:2018-11-14 03:17:14
【问题描述】:
当有空单元格时,我在对列进行排序时遇到问题。
我为我的codeMed 列创建了一个新的Comparator:
codeMed.setComparator(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 == null)return -1;
if (o2 == null) return -1;
return o1 < o2 ? -1 : o1 == o2 ? 0 :1;
}
});
起初,它似乎工作正常:
但如果我决定对不同的列进行排序,然后对 codeMed 列进行排序,就会发生这种情况:
我想错误出在比较器中,但我不知道是什么问题。
编辑:我希望空值始终位于列的底部。我试过这样的事情:
if (codeMed.getSortType() == TableColumn.SortType.DESCENDING) {
return (o1 != null ? o1 : Integer.MAX_VALUE) - (o2 != null ? o2 : Integer.MAX_VALUE);
} else if (codeMed.getSortType()==TableColumn.SortType.ASCENDING){
return (o1 != null ? o1 : Integer.MIN_VALUE) - (o2 != null ? o2 : Integer.MIN_VALUE);
}
return 0;
但它不起作用:/(可能是因为 Slaw 建议的问题)
我的解决方案:
非常感谢 Jai,我调整您的代码只是因为我想将它用于 2 个不同的列:
Comparator<Integer> integerComparator = new Comparator<>() {
@Override
public int compare(Integer o1, Integer o2) {
final boolean isDesc = tabInfosPatient.getSortOrder().get(0).getSortType() == TableColumn.SortType.DESCENDING;
if (o1 == null && o2 == null) return 0;
else if (o1 == null) return isDesc ? -1 : 1;
else if (o2 == null) return isDesc ? 1 : -1;
else return Integer.compare(o1, o2);
}
};
【问题讨论】: