Skip to content

Sort a collection based on each item knowing the item before it

Notifications You must be signed in to change notification settings

mishguruorg/prev-sort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Prev Sort

Sort a collection based on each item knowing the item before it

Installation

npm install --save prev-sort

Usage

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 },
]
*/

About

Sort a collection based on each item knowing the item before it

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published