Is there a way to compare 2 Strings without it being case-sensitive?

Viewed 951

I've just spent almost hour debugging a List<String> in my code which was being sorted wrong by my Comparator.

Turns out that string.compareTo(string2) is case-sensitive. Meaning that all Capital letters come before the lowercase letters. e.g. "Z" comes before "d".

Is there any better way of comparing 2 Strings inside a Comparatorand sorting them alphabetically ascending without them being case sensitive other then string.toLowerCase().compareTo(string2.toLowerCase()); ?

Edit: There's a possibility of any accented letter appearing in my String like for example: ä, ö, ü, é, è, etc.

4 Answers

Use String.CASE_INSENSITIVE_ORDER.compare

compareToIgnoreCase

The String Api has a second compare to function that does a compare while ignoring case.

string1.compareToIgnoreCase(string2);
Related