beforesort

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

npm install beforesort
3 downloads in the last week
5 downloads in the last month

beforesort

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

Installation

$ npm install beforesort

API

require('beforesort')(object, collection, options)

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.

Example

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'
})
npm loves you