tags:

views:

427

answers:

2

I need to sort a List based on MyDto.name in client-side GWT code. Currently I am trying to do this...

Collections.sort(_myDtos, new Comparator<MyDto>() {

        @Override
        public int compare(MyDto o1, MyDto o2) {
        return o1.getName().compareTo(o2.getName());
        }
});

Unfortunately the sorting is not what I was expecting as anything in upper-case is before lower-case. For example ESP comes before aESP.

+1  A: 

That's because capital letters come before lowercase letters. It sounds like you want a case insensitive comparison as such:

Collections.sort(_myDtos, new Comparator<MyDto>() {

        @Override
        public int compare(MyDto o1, MyDto o2) {
        return o1.getName().toLower().compareTo(o2.getName().toLower());
        }
});

toLower() is your friend.

Jason Nichols
This would work as well but String.CASE_INSENSITIVE_ORDER seems to work best.
Benju
And I might add that it would be better/easier to use `compareToIgnoreCase` http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#compareToIgnoreCase(java.lang.String), though the results should be the same.
Igor Klimer
+4  A: 

This is the bad boy you want: String.CASE_INSENSITIVE_ORDER

Tom