2016-06-20 58 views
0

我使用的是GWT CellTable,如果列包含NULL细胞以及细胞下面的代码不排序正确不在NULL:GWT CellTable不排序正确,如果列包含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); 

例如:

黑色,null,null,红色 - 什么都不做。它应该返回 - null,null,黑色,红色在第一选择和红色,黑色,空,空 - 第二选择

黑色,红色,布朗,Tawney - 返回 - 黑色,棕色,红色,Tawney第一选择并且 - Tawney,红色,布朗,黑色 - 在第二选择(即,没有零工作)。

我有几乎相同的代码,指的是不包含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; 
+0

*下面的代码没有正确排序* - 它错误地排序的一个例子以及你想如何排序的例子会帮助任何试图帮助你的人。 –

回答

1

有在你的代码的两个问题。

首先,空检查是在错误的地方:o1 == o2将通过例外,如果o1为空。它应该是:

// 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()是否为空。

+1

你在SuperDev模式下的浏览器应该告诉你*为什么排序不起作用。排序确实有效,但您的代码会抛出异常并终止。看着这样的例外会为你节省大量的时间。 –

+0

这是我从你的建议中得出的结论: – Glyn

+0

//比较六栏。 \t \t如果(01!= NULL){ \t \t如果(01 == O 2){ \t \t返回0; \t \t} \t \t如果(o1.getsixPatrol()!= NULL){ \t \t \t如果(o1.getsixPatrol()== o2.getsixPatrol()){ \t \t \t返回0; \t \t \t} \t \t \t return o2!= null? o1.getsixPatrol()。compareTo(o2.getsixPatrol()):1; \t \t \t} \t \t} \t \t \t 返回\t -1; – Glyn