In this chapter, you learned about the basics of sorting collections in Immutable.js. The basic sort()
and reverse()
methods sort values using a default comparator function. You can supply your own comparator functions for more complex scenarios. There's also the sortBy()
method that accepts an iteratee function, which is used to return values that are passed to the comparator.
We then addressed the issue of sorting maps. Because they're a keyed collection, they have no inherent mechanism to keep track of the order of values. Lists, on the other hand, have this value built into them. Ordered maps are used to preserve the ordering of maps. You can also sort maps by keys if you use sortBy()
to return the key.
We looked at the problem of maintaining the sort order of collections. Having to sort collections every time they change can be inefficient. To address this, you can leverage the sort index approach. On the other hand, this can be difficult to maintain and sort()
is good enough...