Comparing and sorting Strings might not work out like you expected specially when your dealing with a language other than English.
Normally comparing strings in done with simple Unicode ordering. If you want to have localized ordering (the kind expected by an end user) you need to use a Collator.
Commonly used String methods such as:
- String.equalsIgnoreCase(String)
- String.compareTo(String)
can have unexpected result depending on the context you’re using them. The reason is that programmers tend to apply them to tasks they really aren’t meant for, simply out of habit.
The fundamental difference is that localized comparison depends on Locale, while String is largely ignorant of Locale.
The only robust way of doing localized comparison or sorting of Strings, in the manner expected by an end user, is to use a Collator, not the methods of the String class.
For more info check out the code example at http://www.javapractices.com or at http://www.guru99.com/java-strings.html
Recent Comments