BackboneJS和AngularJS排序是否稳定?

zbi*_*big 0 sorting algorithm backbone.js angularjs

我想在我的应用程序中使用BackboneJS和AngularJS.但这些框架中的排序算法是否稳定?例如:它们是否会保留表/集合中先前排序列的顺序?

mu *_*ort 6

我不知道Angular,但Backbone可能会或可能不会有稳定的排序,具体取决于你如何使用它.

所有的集合排序都会通过Backbone.Collection.prototype.sort,如果你看一下,你会看到:

if (_.isString(this.comparator) || this.comparator.length === 1) {
  this.models = this.sortBy(this.comparator, this);
} else {
  this.models.sort(_.bind(this.comparator, this));
}
Run Code Online (Sandbox Code Playgroud)

如果你的集合comparator是一个字符串(即一个模型属性名称)或一个只需要一个参数的函数,那么_.sortBy将进行排序和_.sortBy

返回(稳定)排序的列表副本,...

如果你看一下实现,你会发现通过使用元素索引作为辅助排序键来强制执行稳定性.

如果集合comparator不能与之一起使用_.sortByArray.prototype.sort使用并且不能保证稳定性.