https://github.com/mishguruorg/prev-sort
Sort a collection based on each item knowing the item before it
https://github.com/mishguruorg/prev-sort
library
Last synced: over 1 year ago
JSON representation
Sort a collection based on each item knowing the item before it
- Host: GitHub
- URL: https://github.com/mishguruorg/prev-sort
- Owner: mishguruorg
- Created: 2017-09-15T03:48:30.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T01:34:00.000Z (over 3 years ago)
- Last Synced: 2025-04-01T03:01:29.476Z (over 1 year ago)
- Topics: library
- Language: TypeScript
- Homepage:
- Size: 1.01 MB
- Stars: 0
- Watchers: 4
- Forks: 1
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Prev Sort
> Sort a collection based on each item knowing the item before it
## Installation
```shell
npm install --save prev-sort
```
## Usage
```javascript
import prevSort from 'prev-sort'
const array = [
{ id: 4, previousId: 3 },
{ id: 2, previousId: 1 },
{ id: 3, previousId: 2 },
{ id: 1, previousId: null },
]
const sortedArray = prevSort(array, {
getId: (item) => item.id,
getPreviousId: (item) => item.previousId,
})
/*
[
{ id: 1, previousId: null },
{ id: 2, previousId: 1 },
{ id: 3, previousId: 2 },
{ id: 4, previousId: 3 },
]
*/
```