profileRyan KesPGP keyI build stuffEmailGithubTwitterLast.fmMastodonMatrix

.sort() is guaranteed to be stable

Description

If elements that are considered equal by sorting then sorting does not change the order of those elements.

Example

const arr = [
  { key: "b", value: 1 },
  { key: "a", value: 2 },
  { key: "b", value: 3 },
]
arr.sort((x, y) => x.key.localeCompare(y.key, "en-US"))
console.log(arr) // [ { key: 'a', value: 1 }, { key: 'b', value: 1 }, { key: 'b', value: 3 } ]