答案 0 :(得分:3)
我在项目中所做的是覆盖mixin(https://github.com/emberjs/ember.js/blob/master/packages/ember-runtime/lib/mixins/sortable.js#L52)的orderBy函数,并用这种自然排序算法替换Ember.compare():https://github.com/overset/javascript-natural-sort
orderBy: function (item1, item2) {
var result = 0,
sortProperties = this.get('sortProperties'),
sortAscending = this.get('sortAscending');
Ember.assert("you need to define `sortProperties`", !!sortProperties);
sortProperties.forEach(function (propertyName) {
if (result === 0) {
naturalSort.insensitive = true;
result = naturalSort(Ember.get(item1, propertyName), Ember.get(item2, propertyName));
if ((result !== 0) && !sortAscending) {
result = (-1) * result;
}
}
});
return result;
}
我制作了一个PR,以便更容易在可排序的mixin中插入任何排序函数,但它已关闭。详情请见:
https://github.com/emberjs/ember.js/pull/1216 https://github.com/emberjs/ember.js/pull/1562
答案 1 :(得分:2)
您必须定义其他“自然”属性
App.Group = DS.Model.extend
name: DS.attr 'string'
natural_name: ( ->
# Split string into numeric and non-numeric parts
# and convert numeric parts to actual numbers
# Sort by resulting array of strings and numbers
@get('name').split(/([0-9]+)/g).map (str) =>
if str.match(/[0-9]+/) then parseInt(str, 10) else str
).property('name')
App.GroupsIndexController = Ember.ArrayController.extend
sortProperties: ['natural_name']
答案 2 :(得分:1)
如果你想使用SortableMixin,你可以这样做:
childrenSorted: function() {
return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
sortProperties: ['name'],
content: this.get('model.children')
});
}.property('model.children.@each.name')
答案 3 :(得分:0)
从Ember Data 1.0开始(可能在之前),您可能想要使用Ember.computed.sort
: