Locale-Independent String Sorting in Java
15 Dec 2014In Java programming language, Arrays.sort
or Collections.sort
methods from the Class Library are used to sort a list of strings. These
methods use the compareTo
method of the Comparable
interface for
objects. The standard compareTo
implementation of the String class uses
ASCII codes of the characters, and this works great in English.
When you need to sort strings in languages other than English, or more commonly you need to write a software for global use, then you can use the Collator class.
Below is the Turkish alphabet in order.
Let’s sort the characters of the alphabet with Arrays.sort
method.
The result is not ordered in Turkish, as expected.
Let’s try the Collator class.
This works just in Turkish unsurprisingly. When you need to make your code work
in other languages too, you can use getDefault
instead of
forLanguageTag
.
I will write about internationalization in detail, but until then, have a look at What’s wrong with Turkey? by Jeff Atwood