Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hackergrrl/sort-subset
sort a subset of an array in-place
https://github.com/hackergrrl/sort-subset
Last synced: 20 days ago
JSON representation
sort a subset of an array in-place
- Host: GitHub
- URL: https://github.com/hackergrrl/sort-subset
- Owner: hackergrrl
- Created: 2016-11-05T23:30:35.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2016-11-07T01:20:33.000Z (about 8 years ago)
- Last Synced: 2024-10-10T16:09:23.437Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# sort-subset
> Sort a subset of an array in-place.
## Usage
Let's sort an array with heterogenous types in-place, without disturbing other
types' sort order.```js
var subsort = require('sort-subset')var data = [
{ type: 'apple', id: 7 },
{ type: 'apple', id: 12 },
{ type: 'orange', id: 17 },
{ type: 'apple', id: 2 },
{ type: 'orange', id: 9 }
]var filter = function (elm) { return elm.type === 'apple' }
var sort = function (a, b) { return a.id - b.id }
subsort(data, filter, sort)console.log(data)
```will output
```
[
{ type: 'apple', id: 2 },
{ type: 'apple', id: 7 },
{ type: 'orange', id: 17 },
{ type: 'apple', id: 12 },
{ type: 'orange', id: 9 }
]
```## API
```js
var subsort = require('sort-subset')
```### subsort(array, filterFn, compareFn)
Sorts the array `array` in-place. The function `filterFn` is used to compute the
array subset to sort (signature `function (element, idex)`), and `compareFn` is
the function used to compare elements to perform the sort (signature `function
(a, b)`).## Install
With [npm](https://npmjs.org/) installed, run
```
$ npm install sort-subset
```## License
ISC