String based sorting in GWT

I need to sort the list based on MyDto.name in the client GWT code. I'm currently 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, sorting is not what I expected, because anything is in upper case before lower case. For example, ESP comes before aESP.

+4
source share
3 answers

This is the bad boy you want: String.CASE_INSENSITIVE_ORDER

+12
source

This is because capital letters come to lowercase letters. It looks like you want case insensitivity 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.

+2
source

I usually go for this:

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

Because it seems to me that any string manipulations that you otherwise did (toLower () or toUpper ()) turned out to be less efficient. This way, at least you are not creating two new lines.

+1
source

Source: https://habr.com/ru/post/1299575/


All Articles