Sorting strings in a locale-sensitive way

This article was originally written in English. Text in other languages was provided by machine translation.

In this section, you will learn how to sort strings in a locale-sensitive way.

Before Flash Player 10.1 was released, the sorting API in AS3 was using a locale-independent way which means the APIs are not locale aware. This is not user-friendly for international users who don’t use English as their first/only language. Let’s look at the following example code for some Chinese data:

If we use the APIs in Flash Player 10.0 and earlier to sort the strings:

Actually, the output result is in simple Unicode code point order for the conventional sorting, which is not correct for Chinese users.

With Flash Player 10.1, we can easily sort strings in a locale-sensitive way with the new flash.globalization.collator class. For handling strings sorting and searching, flash.globalization.* package provides Collator class. This class performs locale-sensitive string sorting and searching, or you can say, it allows performing string sorting and searching for different languages.

In the following example, we have instantiate a Collator object with a specific locale. Then we have called the compare method of the Collator class to perform a locale-sensitive string sorting. The sort method returns a negative value if the first string argument is less than the second, a positive value if the first string argument is greater than the second and 0 if the first string argument is equal to the second one.

Now, the output result looks great for Chinese users.

If you want to learn more about Flash Player 10.1 globalization API, please check the following package:

flash.globalization

2 thoughts on “Sorting strings in a locale-sensitive way

    • Yes, Linghong is right. I just updated the blog by fixing the locale value from “zh-Hant” to “zh-Hans”. The original one is using traditional-Chinese.

Leave a Reply