开发者

Best way to maintain a sorted list in javascript

I need to maintain a list of blocks in JavaScript and render them in a specific order. There could be add , delete update ope开发者_StackOverflow社区rations on the list. This list could hold up to 1000 objects.

At the time of render, the list should be rendered in a specific order.

I am looking for the best way to implement n maintain order/sorted list in JavaScript. I need to perform the following operations on this list : Add delete; update and search.

Is there any Jquery or any other library available that can do this?

Thanks for you help.


You could implement your own Linked List in Javascript very easily.


What you need is a simple sort-lookup table. Assuming the elements have unique IDs, use something like this:

var myList = []
myList[position] = elementId

Then you manage the array, not the elements. Read out the array in its native order and write your HTML from that. The rest is simple array manipulation.


Backbone.js might get you there. It has models, collections, and views. You can specify a sort function for collections, which keeps the collection's list of models sorted. The collection emits signals for create, update, and delete events. You can then write a view that renders the collection of models in sorted order when any of the create, update, or delete events are emitted. The models don't necessarily have to correspond to server-side models.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜