I don't believe this is a duplicate of this How does Javascript's sort() work?
let arr = [3, 4, 2, 1];
arr.sort((second,first) => {
console.log([first, second]);
if (first>second) {
return -1; // switch them
}
return 0; // don't switch them
});
console.log(arr);
This returns
[ 3, 4 ]
[ 4, 2 ]
[ 4, 2 ] <---- Why is this output twice?
[ 3, 2 ]
[ 3, 1 ]
[ 2, 1 ]
[ 1, 2, 3, 4 ]
I am trying to figure out what algorithm NodeJS (14.4.0) is using for Array.sort with my input?