如果列包含 NULL 单元格,则 GWT CellTable 无法正确排序
GWT CellTable does not sort correctly if the column contains NULL cells
我正在使用 GWT CellTable,如果该列包含 NULL 单元格以及非 NULL 单元格,则以下代码无法正确排序:
columnSortHandler.setComparator(sixColumn, new Comparator<SectDtlsString>() {
@Override
public int compare(SectDtlsString o1, SectDtlsString o2) {
if (o1 == o2) {
return 0;
}
// Compare the Six columns.
if (o1 != null) {
return (o2 != null) ? o1.getsixPatrol().compareTo(o2.getsixPatrol()) : 1;
}
return -1;
}
});
table.addColumnSortHandler(columnSortHandler);
例如:
Black, null, null, Red - 什么都不做。它应该 return - null, null, Black, Red 第一个 select 和 Red, Black, null, null - 第二个 select
黑色、红色、棕色、茶色 - returns - 第一个是黑色、棕色、红色、茶色 select 和 - 茶色、红色、棕色、黑色 - 第二个 select(即没有空值)。
我有几乎相同的代码引用不包含 NULL 的列并且它们排序非常好。我从教程中复制了这段代码。
这是建议后的结果:
// Compare the Six columns.
if (o1 != null) {
if (o1 == o2) {
return 0;
}
if (o1.getsixPatrol() != null) {
if (o1.getsixPatrol() == o2.getsixPatrol()) {
return 0;
}
return o2 != null ? o1.getsixPatrol().compareTo(o2.getsixPatrol()) : 1;
}
}
return -1;
你的代码有两个问题。
首先,null 检查在错误的地方:o1 == o2
如果 o1 为 null,将通过异常。应该是:
// Compare the Six columns.
if (o1 != null) {
if (o1 == o2) {
return 0;
}
return o2 != null ? o1.getsixPatrol().compareTo(o2.getsixPatrol()) : 1;
}
其次,仅检查 o1 和 o2 不为空是不够的。在比较它们之前,您还需要检查 o1.getsixPatrol()
和 o2.getsixPatrol()
是否不为空。
我正在使用 GWT CellTable,如果该列包含 NULL 单元格以及非 NULL 单元格,则以下代码无法正确排序:
columnSortHandler.setComparator(sixColumn, new Comparator<SectDtlsString>() {
@Override
public int compare(SectDtlsString o1, SectDtlsString o2) {
if (o1 == o2) {
return 0;
}
// Compare the Six columns.
if (o1 != null) {
return (o2 != null) ? o1.getsixPatrol().compareTo(o2.getsixPatrol()) : 1;
}
return -1;
}
});
table.addColumnSortHandler(columnSortHandler);
例如:
Black, null, null, Red - 什么都不做。它应该 return - null, null, Black, Red 第一个 select 和 Red, Black, null, null - 第二个 select
黑色、红色、棕色、茶色 - returns - 第一个是黑色、棕色、红色、茶色 select 和 - 茶色、红色、棕色、黑色 - 第二个 select(即没有空值)。
我有几乎相同的代码引用不包含 NULL 的列并且它们排序非常好。我从教程中复制了这段代码。
这是建议后的结果:
// Compare the Six columns.
if (o1 != null) {
if (o1 == o2) {
return 0;
}
if (o1.getsixPatrol() != null) {
if (o1.getsixPatrol() == o2.getsixPatrol()) {
return 0;
}
return o2 != null ? o1.getsixPatrol().compareTo(o2.getsixPatrol()) : 1;
}
}
return -1;
你的代码有两个问题。
首先,null 检查在错误的地方:o1 == o2
如果 o1 为 null,将通过异常。应该是:
// Compare the Six columns.
if (o1 != null) {
if (o1 == o2) {
return 0;
}
return o2 != null ? o1.getsixPatrol().compareTo(o2.getsixPatrol()) : 1;
}
其次,仅检查 o1 和 o2 不为空是不够的。在比较它们之前,您还需要检查 o1.getsixPatrol()
和 o2.getsixPatrol()
是否不为空。