问题
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