arrays - 按属性对EmberJS对象数组进行排序

标签 arrays sorting object properties ember.js

我在EmberJS网站上的“过滤”下注意到了这一点:

Another common task to perform on an Enumerable is to take the Enumerable as input, and return an Array after sorting or filtering it based on some criteria.



假设我有一个Ember对象数组,如何按属性对它们进行排序?
App.DemoArray = Ember.ArrayController.create({
    content:[
        Ember.Object.create({name:'Joe', Age:29}),
        Ember.Object.create({name:'Jim', Age:53}),
        Ember.Object.create({name:'Jack', Age:12})
    ]
})

如果我想按年龄对以上内容进行排序怎么办?谢谢你的帮助!

编辑:
我在萌芽核心文档中发现了这一点,但似乎不适用于Ember:

You can sort an Enumerable based on the value of some property or list of properties using sortProperty. If you pass in multiple properties, SproutCore will sort items with the same value for the first property by the value of the second parameter, and so on.



本页第3.8节:http://guides.sproutcore20.com/enumerables.html

最佳答案

As described here,您现在可以对ArrayController进行排序。

方法是在ArrayController上提供额外的属性(从上面的链接粘贴):

songs = [
  {trackNumber: 4, title: 'Ob-La-Di, Ob-La-Da'},
  {trackNumber: 2, title: 'Back in the U.S.S.R.'},
  {trackNumber: 3, title: 'Glass Onion'},
];

songsController = Ember.ArrayController.create({
  content: songs,
  sortProperties: ['trackNumber'],
  sortAscending: true
});

songsController.get('firstObject');  // {trackNumber: 2, title: 'Back in the U.S.S.R.'}
songsController.addObject({trackNumber: 1, title: 'Dear Prudence'});
songsController.get('firstObject');  // {trackNumber: 1, title: 'Dear Prudence'}

关于arrays - 按属性对EmberJS对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9041505/

相关文章:

python - 有没有办法检查两个对象在 python 中的每个变量中是否包含相同的值?

c++ - 抽象类和对象

c# - 二维字符串数组中 int 值的平均数组

mysql - 将数据保存为数组还是多行?哪种方式更有效率?

java - 如何计算归并排序中的比较次数?

java - 在 SWT/Eclipse RCP 中对基本树进行排序

java - 返回随机字符串数组

JavaScript:比较两个数组并返回匹配项的索引

r - 在R中按升序对行数据进行排序

Delphi - 从字符串创建类