Backbone.js的多个字段集合比较排序? [英] Backbone.js collection comparator sort by multiple fields?

查看:75
本文介绍了Backbone.js的多个字段集合比较排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  this.col = Backbone.Collection.extend({
    model: M,
    comparator: function(item) {
      return item.get("level");
    }
  });

这上面code排序按级别项目。我想通过水平进行排序,然后按标题。我能做到这一点?谢谢你。

This above code sorts items by level. I want to sort by level, then by title. Can I do that? Thanks.

推荐答案

@ amchang87的答案肯定工作,但另一个我找到方法加工的仅仅是返回排序字段的数组:

@amchang87's answer definitely works, but another that I found worked is simply returning an array of the sortable fields:

this.col = Backbone.Collection.extend({
    model: M,
    comparator: function(item) {
      return [item.get("level"), item.get("title")]
    }
});

我已经在多个浏览器,因为我觉得它依赖于JS的排序顺序为数组的行为(根据它们的内容),没有测试呢。这肯定工作在WebKit的。

I haven't tested this in multiple browsers yet as I think it relies on JS' behavior in sort order for arrays (based on their contents). It definitely works in WebKit.

这篇关于Backbone.js的多个字段集合比较排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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