If your application audience is limited to people who speak English,
you can probably perform string comparisons with the
String.compareTo
method. The
String.compareTo
method performs a binary comparison of
the Unicode characters within the two strings. For most languages,
however, this binary comparison cannot be relied on to sort strings,
because the Unicode values do not correspond to the relative order of
the characters.
Fortunately the
Collator
class allows your application to perform string comparisons for
different languages. In this section, you'll learn how to use the
Collator
class when sorting text.
Collation rules define the sort sequence of strings.
These rules vary with locale,
because various natural languages sort words differently.
Using the predefined collation rules provided by the
Collator
class, you can sort strings
in a locale-independent manner.
In some cases, the predefined collation rules provided by theCollator
class may not work for you. For example, you may want to sort strings in a language whose locale is not supported byCollator
. In this situation, you can define your own collation rules, and assign them to aRuleBasedCollator
object.
With theCollationKey
class, you may increase the efficiency of string comparisons. This class convertsString
objects to sort keys that follow the rules of a givenCollator
.