views:

666

answers:

3

Hi,

is there any way to sort Strings correctly in different languages than English? In German we have Umlaute, and e.g. 'ä' should come right after 'a' in the ascending sort order.

I am using ObjectUtil.stringCompare(), but it puts those special characters always to the end. Any ideas how to solve this? I thought the locale (de_DE) would take care of it, but it does not.

Thanks, Martin

A: 

You can write your own compare function and pass it to array.sort(compareFunction). compareFunction(a, b):int should compare two strings and return -1 if a > b, 0 if a=b, and 1 if a < b. In that function you'll want to compare your strings symbol-by-symbol, taking in account special german symbols.

Hrundik
A: 

I don't know AS3, but almost all language that supports Locale should have locale aware comparison function for string or sorting too.

For Example, here is the one of locale aware string comparison localeCompare().

S.Mark
A: 

In ECMAScript Third Edition (and hence both ActionScript and current browser JavaScript) there is the string.localeCompare method. This does a comparison that depends on the current client locale. For example if I set my system locale (in Windows terms, “language to match the language version of the non-Unicode programs you want to use”) to “German (Germany)” and put javascript:alert('ä'.localeCompare('b')) I get -1, but with English I get 1.

It's generally questionable to depend on the client end locale though. Your application would work differently depending on the client OS installation, and it is not nearly as easy for the user to change their system locale as it is to choose a different language in the web browser prefs UI. I'd avoid it if at all possible, and either:

  • do an ad-hoc string replacement (eg. ä with ae) before comparison. This may be OK if you are only worried about a few umlauts, but is unfeasible for covering the whole of Unicode... even the whole of the Latin diacritical set.

  • try to do the comparison on the server side, in a scripting language with better character model support than ECMAScript.

bobince