中的Array.sort()时产生意外结果时元素是平等的吗? [英] Array.sort() is producing unexpected results when elements are equal?

查看:119
本文介绍了中的Array.sort()时产生意外结果时元素是平等的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heyo!

我想数组排序,有时可能是完全平等的。该功能完美的作品当数组是不平等的,但似乎随机放置元素时,它是完全平等的。例如,我希望下面的code打印A,B,C ...',而是得到的东西,如:K,A,C,D ......。对于sort()函数这一预期的行为吗?我怎么能生产出A,B,C ......的功能?谢谢!

  VAR arrayToSort = [
  {名称:'A',强度:1},{名称:'B',强度:1},{名称:'C',强度:1},{名称:'D',强度:1},
  {名称:'E',强度:1},{名称:'F',强度:1},{名称:'G',强度:1},{名称:'H',强度:1},
  {名称:'我',强度:1},{名称:'J',强度:1},{名称:'K',强度:1},{名称:'L',强度:1},
  {名称:'M',强度:1},{名称:'N',强度:1},{名称:'O',强度:1},{名称:'P',强度:1},
  {名称:'Q',强度:1},{名称:'R',强度:1},{名称:'S',强度:1},{名称:'T',强度:1}
];arrayToSort.sort(功能(A,B){
  返回b.strength - a.strength;
});arrayToSort.forEach(函数(元素){
  的console.log(element.name);
});


解决方案

排序算法留下了比较原来的列表顺序作为平等的被称为元素的属性稳定性。 JavaScript的规范明确允许实现使用不稳定的排序算法。

从规格:


  

的排序是未必稳定(即,比较相等不一定保持在它们的原始顺序元素)。


现在,你如何修复的你的问题取决于你的情况。如果你知道你喜欢原来的顺序,但没有的内在的,会给你的对象的属性,这些属性通过另一个排序顺序,那么一个简单的方法是通过数组做一个合格并添加包含原始数组索引的另一个属性。然后,您可以使用该索引作​​为辅助排序键。 (如果需要)排序后,最终通可以拔出钥匙。

Heyo!

I'm trying to sort an array that sometimes may be entirely equal. The function works perfectly when the array is unequal, but seems to randomly place elements when it is entirely equal. For example, I would expect the code below to print 'a,b,c...', but instead get something like: 'k, a, c, d ...'. Is this expected behavior for the sort() function? How can I produce the 'a,b,c...' functionality? Thanks!

var arrayToSort = [
  {name: 'a', strength: 1}, {name: 'b', strength: 1}, {name: 'c', strength: 1}, {name: 'd', strength: 1},
  {name: 'e', strength: 1}, {name: 'f', strength: 1}, {name: 'g', strength: 1}, {name: 'h', strength: 1},
  {name: 'i', strength: 1}, {name: 'j', strength: 1}, {name: 'k', strength: 1}, {name: 'l', strength: 1},
  {name: 'm', strength: 1}, {name: 'n', strength: 1}, {name: 'o', strength: 1}, {name: 'p', strength: 1},
  {name: 'q', strength: 1}, {name: 'r', strength: 1}, {name: 's', strength: 1}, {name: 't', strength: 1}
];

arrayToSort.sort(function (a, b) {
  return b.strength - a.strength;
});

arrayToSort.forEach(function (element) {
  console.log(element.name);
});

解决方案

The property of a sorting algorithm that leaves elements that compare as equal in their original list order is called stability. The JavaScript spec specifically allows for implementations to use unstable sort algorithms.

From the spec:

The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order).

Now, how you fix your problem depends on your situation. If you know that you "like" the original order, but there's no intrinsic attributes of the objects that would give you that ordering via another sort, then a simple approach would be to make a pass through the array and add another attribute containing the original array index. You could then use that index as a secondary sort key. A final pass after the sort could remove the key (if desired).

这篇关于中的Array.sort()时产生意外结果时元素是平等的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆