3

Is there any way through which we can sort/compare string in JavaSciprt that are locale-dependent? Like java provides us, "java.text.Collator" class.

Zaki Imtiaz
  • 183
  • 1
  • 2
  • 16

1 Answers1

2

I think you're looking for localeCompare.

Try this for example:

'รค'.localeCompare('a'); // Returns 131 in Chrome
Matthew
  • 15,464
  • 2
  • 37
  • 31
  • What I want is that if there are some strings in some locale, say french and I want to sort them in the alphabetic order that french locale supports (which is different from english locale). Is their any method I can sort them? โ€“ Zaki Imtiaz Aug 25 '11 at 10:55
  • 1
    No, you can't use arbitrary locales. Worse still, the implementation of `localeCompare` seems to differ between browsers. IE appears to use the system locale whereas v8 [ignores](http://code.google.com/p/v8/issues/detail?id=459) this altogether and uses the raw UTF16 values, whatever the system locale.... โ€“ Matthew Aug 25 '11 at 11:22
  • Yes you are right. LocaleCompare() is the only option we can use. It picks up the user locale that is set in control panel>regional and language settings>regional options. โ€“ Zaki Imtiaz Aug 26 '11 at 10:36