好的,所以,Ember 有 Ember.ArrayProxy 和 Ember.SortableMixin,所以,你可以这样做:
var sortedElements = Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
content: yourElements.toArray(),
sortProperties: ['propertyYouWantToSortBy'],
sortAscending: false
});
它还有很多其他选项,你可以看看https://github.com/emberjs/ember.js/blob/master/packages/ember-runtime/lib/mixins/sortable.js那里,你可以找到您可以覆盖的 orderBy 函数:
orderBy: function(item1, item2) {
var result = 0,
sortProperties = get(this, 'sortProperties'),
sortAscending = get(this, 'sortAscending'),
sortFunction = get(this, 'sortFunction');
Ember.assert("you need to define `sortProperties`", !!sortProperties);
forEach(sortProperties, function(propertyName) {
if (result === 0) {
result = sortFunction(get(item1, propertyName), get(item2, propertyName));
if ((result !== 0) && !sortAscending) {
result = (-1) * result;
}
}
});
return result;
}
您甚至可以删除断言,并且不设置 sorProperties,并将其更改为如下内容:
orderBy: function(item1, item2) {
var result = 0,
sortAscending = get(this, 'sortAscending'),
sortFunction = get(this, 'sortFunction');
// implement your sort logic here, I don't know how you want to sort it
result = sortFunction(get(item1, propertyName), get(item2, propertyName));
return result;
}