September 2017
Intermediate to advanced
216 pages
6h 8m
English
Let's use the same approach—converting maps to ordered maps before sorting—to sort our maps by keys:
const mySortedByKeyMap = myMap .toOrderedMap() .sortBy((v, k) => k);mySortedByKeyMap.forEach( (v, k) => console.log('mySortedByKeyMap', `${k} => ${v}`));// -> mySortedByKeyMap four => 4// -> mySortedByKeyMap one => 1// -> mySortedByKeyMap three => 3// -> mySortedByKeyMap two => 2
To sort maps by keys, you can use the sortBy() method to provide an iteratee function that returns the keys. This way, the keys are used by the comparator. The result, as you can see, is that the keys determine the sort order instead of the value.
Read now
Unlock full access