Asorted
Fast sorted array in Javascript. Uses binary search for addition and deletion.
Installation
node.js npm install asorted
Examples
var Asorted = ;var sorted = 4 42 8 16 15 23;console; // [ 4, 8, 15, 16, 23, 42 ]sorted; // 3sorted; // -1sorted; // [ 4, 8, 15, 23, 42 ]sorted; // [ 4, 8, 15, 16, 23, 42 ]sorted; // [ 4, 8, 15, 16, 23, 42, 50000 ] var sorted2 = sortBy: 'size' name: 'Danny' size: 10 name: 'Linux' size: 9 ;//[ { name: 'Linus', size: 9 }, { name: 'Danny', size: 10 } ]sorted2;//[ { name: 'Linux', size: 9 },// { name: 'Danny', size: 10 },// { name: 'Jeremey', size: 20 } ]
Geeky stuff
Since binary search is used for adding and deleting, these operations should be in O(log n). This however is just for finding the appropriate index. The splice operation is dependent on the browsers implementation. Modern browsers heavily optimize these operations but the since this uses a dynamic array the worst case could be O(n).
Roadmap
Implemenet sorted arrays of objects. When working with flux.js stores, I normally use native arrays for lists. Coming from backbone.js which has collections which are basically fancy arrays I need a replacement which can keep an array of objects sorted based on a given key. So that's the plan. Pull requests are welcome.- Get this on bower, npm and whatever-package-manger-is-now-cool