In the answers for the Chapter 13 exercises, there are several uses of JavaScript’s array.sort()
to sort an array of numbers. However, this sorts numbers as strings, and so a number like 22
will appear before a number like 3
:
[3, 1, 22].sort();
// [ 1, 22, 3 ], when what we want is [ 1, 3, 22 ]
The answers should replace uses of array.sort()
with:
array.sort((a, b) => (a < b) ? -1 : 1);