Chrome环境和节点环境之间的“排序"方法有什么区别 [英] what's the 'sort' method's difference between chrome environment and node environment

查看:44
本文介绍了Chrome环境和节点环境之间的“排序"方法有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现sort方法显示了在Chrome环境和节点环境中的不同行为

I found the sort method shows different behavior between in Chrome environment and in the node environment

const arr = ['l', 'h', 'z', 'b', 's'];
arr.sort((pre, next) => {
    return pre < next;
});
console.log(arr);

节点环境的结果是 ['z','s','l','h','b'] ,已排序.
chrome控制台环境的结果为 ['l','h','z','b','s'] ,未进行任何更改.
chrome的结果是我所期望的,我不明白为什么它可以在节点环境中工作.

the node environment's result is [ 'z', 's', 'l', 'h', 'b' ], it's sorted.
the chrome console environment's result is ['l', 'h', 'z', 'b', 's'], nothing changed.
chrome's result is what I expect, I don't understand why it working in node environment.

chrome版本为 74.0.3729.169 X64
节点vsrions是 v10.12.0 .

chrome version is 74.0.3729.169 X64
node vsrions is v10.12.0.

推荐答案

V8开发人员在这里.

V8 developer here.

随着一些评论的发表,这与Chrome vs. Node(其行为方式相同)无关.这是由于V8版本的差异所致,其中Chrome 74已经具有新的行为,而Node 10仍然具有旧的行为.更新到Node 11,您将在其中看到相同的行为.

As some of the comments are getting at, this is not about Chrome vs. Node (which should behave the same way). It's due to a difference in V8 versions, where Chrome 74 already has the new behavior whereas Node 10 still has the old. Update to Node 11 and you'll see that same behavior there.

过去,V8使用QuickSort(用于较大的阵列)和InsertionSort(用于小型阵列,最多10个元素)的组合.InsertionSort恰好可以与错误的比较器功能一起正常工作.使用包含11个或更多元素的测试数组,它将不再在Node 10中正确排序.

In the past, V8 used a combination of QuickSort (for larger arrays) and InsertionSort (for small arrays, up to 10 elements). InsertionSort just so happens to work correctly with the bad comparator function. Use a test array with 11 elements or more, and it will no longer sort correctly in Node 10.

(自7.4版开始的V8版本现在将TimSort用于 Array.prototype.sort .)

(Versions of V8 since 7.4 now use TimSort for Array.prototype.sort.)

我知道这不是这个问题,而是为了记录和/或将来其他任何人阅读: (上一个,下一个)=>pre< = next 并不是很好的比较器功能!在JavaScript中, Array.prototype.sort 期望比较器返回的数字小于零,等于零,或大于零,具体取决于第一个参数是否小于,等于或大于第二个参数.因此,对字符串进行排序的正确方法是:

I know that that's not what this question is about, but for the record and/or anyone else reading this in the future: (pre, next) => pre <= next is not a good comparator function! In JavaScript, Array.prototype.sort expects a comparator that returns a number less than zero, equal to zero, or greater than zero, depending on whether the first argument is less than, equal to, or greater than the second. So the proper way to sort strings is something like:

my_string_array.sort((a, b) => {
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
});

使用这样的比较器时,在所有版本的Chrome和Node中,您总是会得到正确的结果.

When you use such a comparator, you will always get the right result, in all versions of Chrome and Node.

当您使用使用单个比较的比较器并因此返回布尔值时, true 静默映射为1,而 false 映射为0,但这意味着它对于实际上不相等的一堆线对意外地返回等于",这会导致非常令人惊讶的排序结果,尤其是当引擎在引擎盖下使用不稳定的排序算法时.

When you use a comparator that uses a single comparison and hence returns a boolean value, then true silently maps to 1 and false maps to 0, but that means it accidentally returns "equal" for a bunch of pairs that are not, in fact, equal, which can lead to quite surprising sorting results, especially when the engine uses an unstable sorting algorithm under the hood.

这篇关于Chrome环境和节点环境之间的“排序"方法有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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