In this line of code which concats two arrays and multiplies each value by 2:
[[3,2,1],[6,5,4]].reduce(function(p,c,i,a) {
return p.concat(c)
}).map(function(v) {
return v * 2
}).sort()
I would expect the final output to be sorted, but it is not. The result is:
[10, 12, 2, 4, 6, 8]
If I put the sort in earlier, it comes back sorted:
[[3,2,1],[6,5,4]].reduce(function(p,c,i,a) {
return p.concat(c)
}).sort().map(function(v) {
return v * 2
})
Result:
[2, 4, 6, 8, 10, 12]
What is going on here? This is not a duplicate question, as noted below. And the duplicate cited is incorrect as well. .sort will indeed sort numbers:
[10,12,2,4,6,8].sort() = [2,4,6,8,10,12]