I need to sort in JS strings alphabetically. In English sorting is quite easy:
sortArr.sort( function ( a , b ) {
return a > b ? 1 : -1;
} );
However the app I am working on will be translated to i.a. Arabic, Chinese, German, Russian and a few others. What I can see that even for non ASCII characters the results seems not to be right.
I found Intl.Collator()pare
according to docs:
The Intl.Collator object is a constructor for collators, objects that enable language sensitive string parison.
However, it's not supported by Safari and most of the mobile browsers.
Is there any other solution I can use?
EDIT
There is String.prototype.localeCompare()
but browser support is not good enough too.
I need to sort in JS strings alphabetically. In English sorting is quite easy:
sortArr.sort( function ( a , b ) {
return a > b ? 1 : -1;
} );
However the app I am working on will be translated to i.a. Arabic, Chinese, German, Russian and a few others. What I can see that even for non ASCII characters the results seems not to be right.
I found Intl.Collator().pare
according to docs:
The Intl.Collator object is a constructor for collators, objects that enable language sensitive string parison.
However, it's not supported by Safari and most of the mobile browsers.
Is there any other solution I can use?
EDIT
There is String.prototype.localeCompare()
but browser support is not good enough too.
-
There was a polyfill attempt, but polyfilling
Intl.Collator
is rather unreasonable: github./andyearnshaw/Intl.js Judging from this bug on the webkit tracker, there seems to be some progress on this issue though: bugs.webkit/show_bug.cgi?id=147601 For many cases, doing server-side sorting might be a viable solution as well. – nils Commented Jan 21, 2016 at 9:35 - @nils Ok I see that it has npm package, I'll look closer. Thanks – LJ Wadowski Commented Jan 21, 2016 at 9:45
-
1
Node.js support should be there. I just tried
Intl.Collator()
in the node repl, it seems to work. – nils Commented Jan 21, 2016 at 9:45 - 1 Given spotty browser support, it seems your options are two: [1] do it on the server-side (may have "performance" issues) or [2] find or write your own client-side collation function for each language you need (may have "ugh" issues). – John Hascall Commented Jan 21, 2016 at 10:04
2 Answers
Reset to default 4Most modern browsers now support Intl.Collator
as of 2020. Even IE11 supports some of its features (although not all). Intl.Collator will sort in almost any language you can think of (certainly more than I can think of) and has numerous options for customizing sorting by accent marks, capitalization, etc.
See this Mozilla Documentation: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#Browser_patibility
This guy gives a really good tutorial on how to use it. https://dev.to/aumayeung/paring-non-english-strings-with-javascript-collators-57bf
Short answer is no.
Every language has its own alphabetic order. For example
Russian has the letter 'с' in a different order than Turkish.
Your only option imo is to use the Collator when possible (browsers that support it). Afaik there aren't any other ready-to-go libraries or object that you can use to achieve this.