beforesort

Sort an item in a collection relative to other items based on it's before/after attribute

beforesort

Sort an item in a collection relative to other items based on it's before/after attribute

$ npm install beforesort

Specify the object to sort and the collection it is in. Use options to set what kind of sorting to do and what keys to compare.

Options:

type: (defaults to 'before') choose 'before' or 'after' compareKey: (defaults to 'id') The attribute that identifies unqiue objects (it's ID) key: (defaults to the value of type which will be 'before' unless specified) The key to tell what object this object should be sorted relative to.

var checkPosition = require('before-sort')
 
var collection = [
  {id: 1, name: "First item"},
  {id: 2, name: "Second item"},
  {id: 3, name: "Third item"},
  {id: 4, name: "Fourth item"}
]
 
var newItem5 = {
  id: 5,
  name: "Inserted item before third item",
  before: 3
}
 
collection.push(newItem)
 
checkPosition(newItem, collection, {
  type: 'before',
  compareKey: 'id',
  key: 'before'
})