在骨干字母数字排序的集合 [英] Sort a collection alphanumerically in backbone

查看:153
本文介绍了在骨干字母数字排序的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个比较的基础上身份证这样的集合进行排序:

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

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

该输入工作得很好:ID-1,ID-0,ID-2,ID-199进行排序为ID-0,ID-1,ID-2,ID-199

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

但不排序数字在这里:ID-1,ID-0,ID-2,ID-199,ID-99作为ID-0,ID-1,ID-2,ID-199,ID -99 - ID-99应该是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.

推荐答案

作为评论由@suish这是解决的方法是通过执行替换所有非数字部分空字符串。

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 –

这篇关于在骨干字母数字排序的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆