0

I got these code:

           ListHandler<List<String>> columnSortHandler = new ListHandler<List<String>>(
                    list);
            columnSortHandler.setComparator(nameColumn,
                    new Comparator<List<String>>() {
                      public int compare(List<String> o1, List<String> o2) {
                        if (o1 == o2) {
                          return 0;
                        }

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

                table.addColumnSortHandler(columnSortHandler);

When sorting column contains iPhone 1, iPhone 2, iPhone 3... then it sorts correctly ie, iPhone 1, iPhone 2, iPhone 3 for ascending & iPhone 3, iPhone 2, iPhone 1 for decending

But when sorting column contains:


1.92 MP
3.15 MP AF 0.31 MP
3.2 MP
5 MP AF and flash
1.3 MP
1.3 MP
2 MP (rear); 0.3 MP (front)
1.92 MP AF with flash (rear)

it follows no correct order, it didn't even put the 2 "1.3 MP" cell next to each other. The correct order for the above list should be:
1.3 MP
1.3 MP
1.92 MP
1.92 MP AF with flash (rear)
2 MP (rear); 0.3 MP (front)
3.15 MP AF 0.31 MP
3.2 MP
5 MP AF and flash

Is there anything wrong with the code above?

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Tom
  • 825
  • 1
  • 8
  • 28

3 Answers3

0

As the author of website said the java sorting algorithm is based on ASCII.It never works as a human thinks. :) rude but fact.

People sort strings with numbers differently than software does.

Use The Alphanum Algorithm

In the above link download AlphanumComparator.java and use it.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • @Baadshah How does the sorting of numbers explain Tom's issue with not putting the "1.3 MP" cells next to each other? I agree that "1", "2", and "10" will sort in the order "1, 10, 2" using an ASCII sort, but this doesn't explain Tom's problem. – Andy King Apr 11 '13 at 17:09
  • @Tom I think your problem is that you're using the "==" operator when comparing `String` values ... you need to use `String#compareTo` (in fact, I'm not sure why you're writing your own comparator, since the `String#compareTo` will suffice). – Andy King Apr 11 '13 at 17:12
  • @andy are you sure that compareTo will give what tom is asking even with numbers in string..? – Suresh Atta Apr 11 '13 at 17:45
  • @Baadshah `String#compareTo` should work with the example strings that Tom has identified. It won't work for "1", "2", and "10" however. – Andy King Apr 11 '13 at 22:11
0

I also add o1.get(0).compareTo(o2.get(0))==0 into the code as what Any King suggested, but the sorting is still very messy.

ListHandler<List<String>> columnSortHandler = new ListHandler<List<String>>(
                list);
        columnSortHandler.setComparator(nameColumn,
                new Comparator<List<String>>() {
                  public int compare(List<String> o1, List<String> o2) {
                    if (o1.get(0).compareTo(o2.get(0))==0) {
                      return 0;
                    }

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

            table.addColumnSortHandler(columnSortHandler);
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
Tom
  • 825
  • 1
  • 8
  • 28
0

I think the problem is

ListHandler<List<String>>

& not the java sorting algorithm cos look at Gwt Sample http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellTable, it sort the address beautifully & the code use very simple compare. look at sample gwt code for sorting address

 addressColumn.setSortable(true);
addressColumn.setDefaultSortAscending(false);
sortHandler.setComparator(addressColumn, new Comparator<ContactInfo>() {
  @Override
  public int compare(ContactInfo o1, ContactInfo o2) {
    return o1.getAddress().compareTo(o2.getAddress());
  }
});
Tom
  • 825
  • 1
  • 8
  • 28