Roman Ruiz

Full Stack Developer

JavaScript default sort() method is for strings

In JavaScript, [-6,-4,1].sort() = [-4, -6, 1], which is incorrect. The default sort function converts the array into a string and compares string values, which only works for sorting positive numbers.

To correctly sort positive and negative numbers, pass in a comparator function:

[-6,-4,1].sort(function(a, b) { return a - b; })

which returns the (already) sorted array.