Javascript's string comparisons and Array.prototype.sort compares strings unicode code points character by character.
String.prototype.localeCompare and Intl.Collator.prototype.compare both compare strings using locale aware rules. These methods implement the negative, zero, positive interface used by Array.prototype.sort, so they are useful for sorting objects by a field
// nice!
records.sort((a, b) => a.field.localeCompare(b.field))
// boo
records.sort((a, b) => {
if (a.field === b.field) return 0
if (a.field < b.field) return -1
if (a.field > b.field) return 1
})
Is there a locale I can pass to localeCompare or Intl.Collator to make it equivalent to string's >, <, ===?