An array that keeps its elements in order
npm install @shlappas/sorted-arraySometimes you have an array that needs to be kept in order through pushes and
pops etc.
``js
// Becomes slow as arrays get larger. O(n log n)
arr.push(value)
arr.sort()
// May check all values; not binary search.
arr.indexOf(value)
`
This module provides an API similar to the builtin javascript Array, but
remains sorted after insertion and deletion.
`js
const arr = new SortedArray((a, b) => a - b)
// Each insert runs in O(log n)
arr.insert(5) // [ 5 ]
arr.insert(10) // [ 5 10 ]
arr.insert(1) // [ 1 5 10 ]
arr.insert(5) // [ 1 5 5 10 ]
arr.forEach((v) => console.log(v)) // 1 5 5 10
`
Check out the docs!
`bash`
yarn add @shlappas/sorted-array
This modules is very similar to
sorted-set`. (both use a
weight-balanced tree in the background)