且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SomeArray.sort(function(){...})语句背后的逻辑是什么?

更新时间:2023-01-22 22:42:59

该函数实际上接受两个参数,它们是数组中的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