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

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

问题描述

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

以上代码按级别对项目进行排序.我想按级别排序,然后按标题排序.我可以这样做吗?谢谢.

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天全站免登陆