Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: I need a good data structure to keep a sorted list

This would probably be implemented as a tree or something? My point is it needs to be efficient.

I don't know where to find good implementations of data structures for Javascript for something like this, though. I don't want to have to roll my own if I can avoid it.

Help appreciated.

like image 785
Hamster Avatar asked Sep 28 '10 04:09

Hamster


2 Answers

Depends why you need it. For instance, if you only need the top element, this binary heap might be okay for you. Otherwise, implement a binarySearch and insertSorted functions for arrays, should not be more than ten-fifteen lines. Unless you plan on having thousands upon thousands of elements; then it makes more sense to just insert in bulk and then sort using the builtin.

like image 167
Amadan Avatar answered Oct 17 '22 00:10

Amadan


How about a simple array, sorted after each update?

like image 45
HBP Avatar answered Oct 17 '22 00:10

HBP