September 2017
Intermediate to advanced
216 pages
6h 8m
English
Another method shared between native JavaScript arrays and Immutable.js lists is reverse(). It shares the same semantics as well: it reverses the current value order. This means that if you want the values sorted in reverse order, you have to sort them first:
const myList = List.of(2, 1, 4, 3);const myReversedList = myList.reverse();console.log('myList', myList.toJS());// -> myList [ 2, 1, 4, 3 ]console.log('myReversedList', myReversedList.toJS());// -> myReversedList [ 3, 4, 1, 2 ]
This probably isn't what you're after. Since the list wasn't sorted before it was reversed, it doesn't end up in descending order. One answer is to call sort() and reverse():
const mySortedReversedList = myList.sort().reverse();console.log('mySortedReversedList', ...Read now
Unlock full access