September 2017
Intermediate to advanced
216 pages
6h 8m
English
What if you want to sort by both the age and name keys? For example, you want to sort by the age key first. However, there are several maps that have the same age value. This is where you want to compare the name keys. Using sortBy() for this is out of the question because you can't implement this using a single comparison. You have to implement your own comparator function, as follows:
const comp = (order, ...props) => (a, b) => { for (const p of props) { if (a.get(p) > b.get(p)) { return order * 1; } if (a.get(p) < b.get(p)) { return order * -1; } } return 0;};const asc = (...props) => comp(1, ...props);const desc = (...props) => comp(-1, ...props);
The comp() function takes an order argument, either 1 for ascending ...
Read now
Unlock full access