Search code examples
javascriptlocale

A stable/neutral/culture insensitive locale in Javascript?


I would like to have a stable, well defined sort order in Javascript.

It could be any defined locale, but the point is to have the code say "just some neutral sort order used here".

Java and C# have their Neutral culture and ROOT locales which have been used for this purpose.

The Mozilla documentation does not at least immediately seem to offer such options.


Solution

  • As @Bergi pointed out above, in many cases you can just use < on strings and it is well defined and culture insensitive.

    Say for using some kind of custom sort something like this would work:

    ["foo", "bar"].sort((a, b)=> a < b ? -1 : 1)
    

    (in that simple case .sort() would of course be equivalent and simpler.)