骨干网/下划线sortBy未分类收集 [英] Backbone/Underscore sortBy is not sorting collection

查看:105
本文介绍了骨干网/下划线sortBy未分类收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户提供姓名,姓氏的属性集合中的列表(六是准确的)。做一个获取,下面的比较通过'姓'对其进行排序,并能正常工作。

 比较:功能(用户){
  返回user.get(名字)与toLowerCase()。
}

但是,如果我尝试收集后进行排序,通过不同的值,即'姓氏',这是行不通的。顺序保持不变。

  this.collection.sortBy(功能(用户){
  返回user.get(姓氏)与toLowerCase()。
});

我究竟做错了什么?

结果
更新


因此​​,从sortBy返回的数据进行排序,但我的看法是联系在一起的集合,它并不能帮助我真的。如果我重新收集并添加排序数组回收集它的比较做它的工作,并对其排序,返回到姓名的顺序。

  VAR分类= this.collection.sortBy(功能(用户){
  返回user.get(姓氏)与toLowerCase()。
});


解决方案

sortBy 功能不会将当前集合中的对象进行排序。它返回一个排序的集合:

 
VAR sortedCollection = this.collection.sortBy(功能(用户){
  返回user.get(姓氏)与toLowerCase()。
});

现在你可以使用 sortedCollection 键,它会被正确地排序。

I have a list of users (six to be exact) in a collection with 'firstname', 'lastname' properties. Doing a fetch, the comparator below sorts them by 'firstname', and it works fine.

comparator : function (user) {
  return user.get("firstname").toLowerCase();
}

But if I try to sort the collection later, by a different value i.e. 'lastname', it doesn't work. The order stays the same.

this.collection.sortBy(function(user) {
  return user.get("lastname").toLowerCase();
});

What am i doing wrong?


Update


So the data returned from sortBy IS sorted but that doesn't help me really as my view is linked to the collection. If i reset the collection and add the sorted array back to the collection it's comparator does it's job and sorts it back into 'firstname' order.

var sorted = this.collection.sortBy(function(user) {
  return user.get("lastname").toLowerCase();
});

解决方案

The sortBy function does not sort the objects in the current collection. It returns a sorted collection:


var sortedCollection = this.collection.sortBy(function(user){
  return user.get("lastname").toLowerCase();
});

Now you can use sortedCollection and it will be sorted correctly.

这篇关于骨干网/下划线sortBy未分类收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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