sort-map
Sorts a Map by its keys and/or values.
Unlike Array.prototype.sort()
, sort-map
does not sort the Map in-place. It creates a new, sorted Map and returns it.
Installation
Requires Node.js 7.0.0 or above.
npm install sort-map --save
The module exports a single function.
Usage
Maps
By default, sort-map
sorts a Map by its keys:
const sortMap = const map = 'b' 2 'a' 1const sortedMap = Array // ['a', 'b']
You can provide a callback if your sorting needs are more complex. This example sorts the Map by its values:
const compare = const sortMap = const map = 'b' 2 'a' 1const sortedMap = Array // [1, 2]
The above example makes use of the 3
module in the sort callback.
Objects
sort-map
can also sort an Object (but remember that JavaScript technically does not guarantee that Object keys will be enumerated in any particular order).
const sortMap = // {a: 1, b: 2}