Skip to content

Instantly share code, notes, and snippets.

@reaktivo
Last active February 27, 2019 18:24
Show Gist options
  • Save reaktivo/f373f155352d02d6ee07c43281be4dad to your computer and use it in GitHub Desktop.
Save reaktivo/f373f155352d02d6ee07c43281be4dad to your computer and use it in GitHub Desktop.
Node sorting bug
// Run this with node 8
// npx node@8 ./index.js
const arr = [
{ i: 1 },
{ i: 2 },
{ i: 3 },
{ i: 4 },
{ i: undefined },
{ i: undefined },
{ i: undefined },
{ i: undefined },
{ i: undefined },
{ i: undefined },
{ i: undefined }
]
const res1 = arr
.slice()
.sort((a, b) => a.i - b.i)
.map(item => item.i);
const res2 = arr
.slice()
.map(item => item.i)
.sort((a, b) => a - b);
console.log(res1);
console.log(res2);
// On node 8 the results are different
// On node 10 the results are the same
@reaktivo
Copy link
Author

this will output

npx node@8 ./index.js
[ 1,
  undefined,
  3,
  4,
  undefined,
  2,
  undefined,
  undefined,
  undefined,
  undefined,
  undefined ]
[ 1,
  2,
  3,
  4,
  undefined,
  undefined,
  undefined,
  undefined,
  undefined,
  undefined,
  undefined ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment