sort() is the JavaScript array method most likely to surprise you the first time you use it with numbers. The behavior is not a bug. It is the result of a specific design decision that made sense in the context of the original language spec. Understanding why it works this way makes the fix obvious and prevents the mistake from recurring. The Surprise [ 10 , 9 , 100 , 2 , 55 ]. sort (); // [10, 100, 2, 55, 9] Enter fullscreen mode Exit fullscreen mode If you expected [2, 9, 10, 55, 100] , you were expecting numeric sort order. What you got was lexicographic sort order. The array was sorted as if the numbers were strings: "10" comes before "100" because "1" comes before "1" and then "0" comes before "00"... actually "100" starts with "1", "10" starts with "1", and then comparing "0" vs nothing makes "10" come before "100". "2" comes last because "2" > "1" lexicographically. This is not specific to numbers. .sort() with no argument converts each element to a string and sorts by Unicode code point order.…