我在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/