https://github.com/lamansky/sort-map
[Node.js] Sorts a Map by its keys and/or values.
https://github.com/lamansky/sort-map
Last synced: 3 months ago
JSON representation
[Node.js] Sorts a Map by its keys and/or values.
- Host: GitHub
- URL: https://github.com/lamansky/sort-map
- Owner: lamansky
- License: mit
- Created: 2018-01-22T16:25:39.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-22T16:27:56.000Z (over 8 years ago)
- Last Synced: 2025-03-11T17:50:58.609Z (over 1 year ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# sort-map
Sorts a Map by its keys and/or values.
Unlike [`Array.prototype.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Sort), `sort-map` does **not** sort the Map in-place. It creates a new, sorted Map and returns it.
## Installation
Requires [Node.js](https://nodejs.org/) 7.0.0 or above.
```bash
npm install sort-map --save
```
The module exports a single function.
## Usage
### Maps
By default, `sort-map` sorts a Map by its keys:
```javascript
const sortMap = require('sort-map')
const map = new Map([['b', 2], ['a', 1]])
const sortedMap = sortMap(map)
Array.from(sortedMap.keys()) // ['a', 'b']
```
You can provide a callback if your sorting needs are more complex. This example sorts the Map by its values:
```javascript
const compare = require('3')
const sortMap = require('sort-map')
const map = new Map([['b', 2], ['a', 1]])
const sortedMap = sortMap(map, ([k1, v1], [k2, v2]) => compare(v1, v2))
Array.from(sortedMap.values()) // [1, 2]
```
The above example makes use of the [`3`](https://github.com/lamansky/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).
```javascript
const sortMap = require('sort-map')
sortMap({b: 2, a: 1}) // {a: 1, b: 2}
```