Search code examples
javascripttypescriptstring-comparison

Deterministic string compare in JS/TS?


I am looking for a deterministic way of sorting a list of strings.

Sorting a string of course often leads to the suggestion to use String.localeCompare. But the order must be deterministic, unrelated to the computer where it is running on.

The hardcore solution I came up with is hashing each string and compare these instead with a locale option en. Is there an easier solution?

The strings can be English, German, Chinese, Japanese, ...


Solution

  • Oddly, what fits your requirements is...the default sort:

    theStrings.sort();
    

    That sorts according to the UTF-16 code units in the strings, which doesn't vary by computer/locale/whatever. It treats the strings as (effectively) a series of 16-bit numbers (Unicode code units, to be precise).

    From the specification:

    If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative Number if x < y, a positive Number if x > y, or a zero otherwise.

    And the < and > operators are defined by the abstract IsLessThan operation in the specification, which compares by the code units in the string.