1

I was looking at code of CaseInsensitiveComparator but could not understand following piece of code

               if (c1 != c2) {
                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);
                if (c1 != c2) {
                    c1 = Character.toLowerCase(c1);
                    c2 = Character.toLowerCase(c2);
                    if (c1 != c2) {
                        // No overflow because of numeric promotion
                        return c1 - c2;
                    }
                }
            }

What is the purpose of comparing characters in both uppercase and lowercase form? Isn't one of them sufficient? Are we suspecting some characters which can be equal when they are lowercase but not equal when they are uppercase?

Bagira
  • 2,149
  • 4
  • 26
  • 55

1 Answers1

1

From Unicode Standard:

In addition, because of the vagaries of natural language, there are situations where two different Unicode characters have the same uppercase or lowercase

So sometimes you will find that the lowercase of the letters are same but the uppercase is different and hence the comparison.

Also check the source which says:

Unfortunately, conversion to uppercase does not work properly for the Georgian alphabet, which has strange rules about case conversion. So we need to make one last check before exiting.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331