GWT, columnSortHandler.setComparator for all columns in a loop, make the sorting messed-up?

StackOverflow https://stackoverflow.com/questions/16146094

  •  11-04-2022
  •  | 
  •  

문제

I've got this code:

ListHandler<List<String>> columnSortHandler = new ListHandler<List<String>>(list);

for (int k=0; k<10; k++){
         IndexedColumn myColumn=new IndexedColumn(k+1);
         table.addColumn(myColumn, "col "+k);
         myColumn.setSortable(true);

         columnSortHandler.setComparator(myColumn, new Comparator<List<String>>() {

             public int compare(List<String> o1,     List<String> o2) {
                 return o1.get(0).compareTo(o2.get(0));
             }

         });
}

When I do the sorting, the sorting action was invoked, the table did the sorting but the orders of values in that column are not correct. So I suspect it could be I put the columnSortHandler.setComparator inside a loop & that's causing the problem.

How do I fix it?

도움이 되었습니까?

해결책

Your issue is that every time you compare you're comparing on the first String in the list. Assuming every object in the list represents the value of the column of that index then you should just change the index that your getting in the comparator.

Assuming what I said above is right then you might want to take a look at the answer I posted here.

In your case your comparator should be:

columnSortHandler.setComparator(myColumn, new Comparator<List<String>>(){

    public int compare(List<String> o1, List<String> o2) {

          if (o1 == o2) {
            return 0;
          }

          // Compare the column.
          if (o1 != null) {
            return (o2 != null) ? o1.get(k).compareTo(o2.get(k)) : 1;
          }
          return -1;
    }


});

Now I'm not sure if in the code above the k should be just k or k+1. Looking at the code you posted it isn't clear to me if your first column's value is at index 0 or 1.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top