May 2017
Intermediate to advanced
448 pages
10h 10m
English
To perform the sort, we will be taking advantage of JavaScript's built-in .sort() method. It does an in-place sort on an array, and can take a comparator function as an argument. This function compares two items in the array and should return a positive or negative number depending on which item should come first in the sorted array.
For example, take a simple array of numbers:
const arr = [52, 97, 3, 62, 10, 63, 64, 1, 9, 3, 4];
We can sort this array by calling arr.sort(). After this, the items are in the following order:
[1, 10, 3, 3, 4, 52, 62, 63, 64, 9, 97]
By default, as we see here, the items are sorted lexicographically (in alphabetical order). In this case, it might make more sense to sort the ...
Read now
Unlock full access