Sort a collection alphanumerically in backbone

Deadly 提交于 2019-12-12 05:29:40

问题


I have written a comparator to sort the collection based on "id" like this:

comparator: function(coll) {
    return coll.get('id');
}

This works fine for input: "id-1, id-0, id-2, id-199" to sort as "id-0, id-1, id-2, id-199"

But does not sort numerically here: "id-1, id-0, id-2, id-199, id-99" as "id-0, id-1, id-2, id-199, id-99" -- id-99 should be before id-199.


回答1:


As commented by @suish the way this was solved was by performing a replace of all non-numerical part with empty string.

coll.get('id').replace(/[^0-9^\.]/g,"")|0 –


来源:https://stackoverflow.com/questions/33621926/sort-a-collection-alphanumerically-in-backbone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!