Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Ember.js SortableMixin按自然顺序排序?_Javascript_Sorting_Ember.js_Natural Sort - Fatal编程技术网

Javascript 如何使用Ember.js SortableMixin按自然顺序排序?

Javascript 如何使用Ember.js SortableMixin按自然顺序排序?,javascript,sorting,ember.js,natural-sort,Javascript,Sorting,Ember.js,Natural Sort,您必须定义其他“自然”属性 如果要使用SortableMixin,可以这样做: childrenSorted: function() { return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, { sortProperties: ['name'], content: this.get('model.children') }); }.property('model.children.@each.name')


您必须定义其他“自然”属性


如果要使用SortableMixin,可以这样做:

childrenSorted: function() {
  return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
    sortProperties: ['name'],
    content: this.get('model.children')
  });
}.property('model.children.@each.name')

在我的项目中,我所做的是覆盖mixin()的orderBy函数,并用以下自然排序算法替换Ember.compare()

我做了一个PR,以便更容易在可排序的mixin中插入任何排序函数,但它被关闭了。详情如下:

从Ember Data 1.0开始(可能更早),您可能希望使用
Ember.computed.sort

childrenSorted: function() {
  return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
    sortProperties: ['name'],
    content: this.get('model.children')
  });
}.property('model.children.@each.name')
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;
}