Last active
February 27, 2019 18:24
-
-
Save reaktivo/f373f155352d02d6ee07c43281be4dad to your computer and use it in GitHub Desktop.
Node sorting bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this will output