SomeArray.sort(function(){...})语句背后的逻辑是什么? [英] What's the logic behind the SomeArray.sort ( function() { ... } ) statement?

查看:167
本文介绍了SomeArray.sort(function(){...})语句背后的逻辑是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var array = [3,9,23,76,1,54,21,12,0,9,2];
var shuffled = array.sort(function() {return 0.5 - Math.random()});
console.log(shuffled);  

我知道结果,并且对结果感到满意.

I'm aware of the results, and also satified with them.

上面的代码返回数组元素的重新排列顺序.

The code above returns a shuffled order of the elements of my array.

我很困惑为什么它会导致输出结果.

I'm puzzled by why it results in that output.

.sort内部函数的意义是什么?对输出有何贡献?

What's the point of the function inside of the .sort and how does it contribute to the output?

推荐答案

该函数实际上接受两个参数,它们是数组中的2个项目.目的是比较这些元素并返回一个数字.
如果数字为正,则第二项应在第一项之前.
如果数字是0或负数,则第二项应该在第一项之后.

The function actually takes two arguments, which are 2 items from the array. The purpose is to compare these elements and return a number.
If the number is positive, the second item should be before the first item.
If the number is 0 or negative, the second item should be after the first item.

[0,1].sort(function(a,b){return  1;}); // [1, 0], reverses order
[0,1].sort(function(a,b){return  0;}); // [0, 1], does nothing
[0,1].sort(function(a,b){return -1;}); // [0, 1], does nothing 

在上面的示例中,分别为a === 0b === 1.

In each case of the example above, a === 0 and b === 1.

要逐步查看[1,3,2,4,4,0]上的升序情况,可以编写一个函数来准确记录每一步发生的情况

To see step-by-step what is happening with an ascending sort on [1,3,2,4,4,0], one can write a function which logs exactly what happens each step

arr = [1,3,2,4,4,0];
arr.sort(function(a,b){ // ascending order sort
    var result = a-b,
    str = '';
    if(result > 0) str = 'so swapping';
    else if(result === 0) str = 'so ignoring'
    else str = 'so continuing';
    console.log('with [ '+arr.join(', ')+' ]','comparing',a,'to',b,'resulting in',result, str);
    return result;
});
console.log('resulting in [ '+arr.join(', ')+' ]');

输出

with [ 1, 3, 2, 4, 4, 0 ] comparing 1 to 3 resulting in -2 so continuing
with [ 1, 3, 2, 4, 4, 0 ] comparing 3 to 2 resulting in 1 so swapping
with [ 1, 3, 3, 4, 4, 0 ] comparing 1 to 2 resulting in -1 so continuing
with [ 1, 2, 3, 4, 4, 0 ] comparing 3 to 4 resulting in -1 so continuing
with [ 1, 2, 3, 4, 4, 0 ] comparing 4 to 4 resulting in 0 so ignoring
with [ 1, 2, 3, 4, 4, 0 ] comparing 4 to 0 resulting in 4 so swapping
with [ 1, 2, 3, 4, 4, 4 ] comparing 4 to 0 resulting in 4 so swapping
with [ 1, 2, 3, 4, 4, 4 ] comparing 3 to 0 resulting in 3 so swapping
with [ 1, 2, 3, 3, 4, 4 ] comparing 2 to 0 resulting in 2 so swapping
with [ 1, 2, 2, 3, 4, 4 ] comparing 1 to 0 resulting in 1 so swapping
resulting in [ 0, 1, 2, 3, 4, 4 ]


为完整起见,原始问题中随机播放算法的概率表(估计,基于每个索引的500,000次试验),X为起始索引


For completeness, probability table for shuffle algorithm in original question (estimates, based on 500,000 trials for each index), X is starting index

 x     0     1     2     3     4     5     6     7     8     9    10
 0 |  8.0,  8.0,  6.2,  6.6,  9.2, 10.8,  9.3,  6.6,  6.2,  9.7, 18.8
 1 |  4.5,  4.6,  7.8, 12.2, 16.9, 12.9, 11.4, 10.7,  8.7,  6.1,  3.6
 2 | 15.5, 15.5, 10.3,  5.9,  3.7,  3.8,  5.7,  8.3, 10.6, 11.7,  8.5
 3 | 10.4, 10.3, 13.4, 10.2,  7.0,  6.5,  7.8,  9.4,  9.7,  8.8,  6.0
 4 |  6.4,  6.3, 10.7, 15.4, 11.4,  9.5,  9.6,  9.9,  8.9,  6.9,  4.4
 5 | 16.1, 16.1, 10.9,  7.7,  7.4,  7.6,  6.2,  4.4,  4.1,  6.5, 12.5
 6 |  4.7,  4.7,  7.1,  9.7, 12.6, 16.3, 13.6, 11.9,  9.2,  6.1,  3.6
 7 |  6.0,  6.0,  7.7,  8.9,  9.4, 10.9, 14.0, 13.6, 11.2,  7.4,  4.3
 8 |  8.4,  8.3,  9.1,  8.6,  7.3,  6.7,  8.2, 11.6, 13.8, 10.8,  6.7
 9 | 11.5, 11.4, 10.1,  7.6,  5.0,  3.7,  4.2,  6.7, 10.9, 15.8, 12.6
10 |  8.0,  8.1,  6.2,  6.6,  9.2, 10.9,  9.2,  6.6,  6.1,  9.8, 18.8

这篇关于SomeArray.sort(function(){...})语句背后的逻辑是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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