简体   繁体   English

sort()a和b变量中的JavaScript回调函数

[英]JavaScript callback function inside sort() a and b variables

I am trying to understand how the sort() function works along with the callback function passed into it. 我试图理解sort()函数如何与传递给它的回调函数一起工作。 More specifically the values of a and b 更具体地说, ab的值

Example code: 示例代码:

var n = [4, 11, 2, 10, 3, 1];
    n.sort(function(a, b) {
        console.log(a);
        console.log(b);
        console.log('--')
        return a-b;
    });

Result: 结果:

4
11
--
11
2
--
4
2
--
11
10
--
4
10
--
11
3
--
10
3
--
4
3
--
2
3
--
11
1
--
10
1
--
4
1
--
3
1
--
2
1
--

The first round I can follow that a = 4, and b = 11, easy to follow. 第一轮我可以遵循a = 4, b = 11,易于遵循。

The second round I can follow that a = 11 and b = 2. 第二轮我可以遵循a = 11和b = 2。

But after that I sort of loose track of what actually is going, for example when you get to when a = 4 and b = 3. How is this actually working? 但在那之后我对实际发生的事情进行了一些松散的跟踪,例如当你到达a = 4和b = 3时。这实际上是如何工作的? I tried working it out on paper but could not see the logic in the output of a and b . 我尝试在纸上完成它,但无法看到ab输出中的逻辑。

You can see it this way. 你可以这样看。 When you have two numbers, compare the a (the previous one) and b (the next one). 如果有两个数字,请比较a(前一个)和b(下一个)。
If a is bigger than b, put it after b. 如果a大于b,则将其放在b之后。
If a is smaller than b, put it before b. 如果a小于b,则将其放在b之前。
In fact, when you have the case 'a > b', you can return any positive number: put a after b. 事实上,当你有一个'a> b'的情况下,你可以返回任何正数:放一个后b。
And, when you have the case 'a < b', you can return any negative number: put a before b. 并且,当你有一个'a <b'的情况下,你可以返回任何负数:在b之前放一个。 It is essentially comparing 2 numbers at a time. 它实际上是一次比较2个数字。

The position in the array can be understood as below. 阵列中的位置可以理解如下。 Looking from the perspective of return ab , if you return a negative number, put a before b; return ab的角度来看,如果你返回一个负数,则先放一个b; if you return a positive number, put a after b. 如果你返回一个正数,请输入一个后b。 negative numbers - zero - positive numbers. 负数 - 零 - 正数。

Perhaps, you can understand it better by printing out content in n during runtime. 也许,您可以通过在运行时打印出n中的内容来更好地理解它。

window.n = [4, 11, 2, 10, 3, 1];
n.sort(function(a, b) {
    console.log(a);
    console.log(b);
    console.log(window.n); // You can see what is in n in the every comparison
    console.log('--')
    return a-b;
});

Result on Chrome v64.0.3282 Chrome v64.0.3282上的结果

4
11
(6) [4, 11, 2, 10, 3, 1]
--
11
2
(6) [4, 11, 2, 10, 3, 1]
--
4
2
(6) [4, 11, 11, 10, 3, 1]
--
11
10
(6) [2, 4, 11, 10, 3, 1]
--
4
10
(6) [2, 4, 11, 11, 3, 1]
--
11
3
(6) [2, 4, 10, 11, 3, 1]
--
10
3
(6) [2, 4, 10, 11, 11, 1]
--
4 
3
(6) [2, 4, 10, 10, 11, 1]
--
2
3
(6) [2, 4, 4, 10, 11, 1]
--
11
1
(6) [2, 3, 4, 10, 11, 1]
--
10
1
(6) [2, 3, 4, 10, 11, 11]
--
4
1
(6) [2, 3, 4, 10, 10, 11]
--
3
1
(6) [2, 3, 4, 4, 10, 11]
--
2
1
(6) [2, 3, 3, 4, 10, 11]
--

(6) [1, 2, 3, 4, 10, 11] // result

Your code returns the same result as below: 您的代码返回与以下相同的结果:

var n = [4, 11, 2, 10, 3, 1];
n.sort(function(a, b) {
    console.log(a);
    console.log(b);
    console.log('--')
    if (a > b) {
        return 1;
    } else {
        return -1;
    }  
});
(6) [1, 2, 3, 4, 10, 11] // result

OR 要么

var n = [4, 11, 2, 10, 3, 1];
n.sort((a, b) => a > b ? 1 : -1);
(6) [1, 2, 3, 4, 10, 11] // result

Looks like a modified bubble sort. 看起来像修改后的冒泡排序。 You can see what is happening when you compare the state of the array at each step - I have added them below. 当你比较每一步的数组状态时,你可以看到发生了什么 - 我在下面添加了它们。

Move the value from index 1 as low as it should go. 将索引1中的值尽可能低地移动。 (index 0-1 are in order) Now move the value from index 2 as low as it should go. (索引0-1按顺序)现在将索引2中的值移动到应该的最低值。 (index 0-2 are in order) Now move the value from index 3 as low as it should go. (索引0-2按顺序)现在将索引3中的值移动到应该去的最低位置。 (index 0-3 are in order) (索引0-3是有序的)

Since we know how much of the array is in order we short circuit the comparisons and jump to the next index as soon as the comparison function is not negative. 由于我们知道有多少数组是为了使比较短路并且在比较函数不是负数时立即跳转到下一个索引。

4
11
-- [4, 11, 2, 10, 3, 1];
11
2
-- [4, 2, 11, 10, 3, 1];
4
2
-- [2, 4, 11, 10, 3, 1];
11
10
-- [2, 4, 10, 11, 3, 1];
4
10
-- [2, 4, 10, 11, 3, 1];
11
3
-- [2, 4, 10, 3, 11, 1];
10
3
-- [2, 4, 3, 10, 11, 1];
4
3
-- [2, 3, 4, 10, 11, 1];
2
3
-- [2, 3, 4, 10, 11, 1];
11
1
-- [2, 3, 4, 10, 1, 11];
10
1
-- [2, 3, 4, 1, 10, 11];
4
1
-- [2, 3, 1, 4, 10, 11];
3
1
-- [2, 1, 3, 4, 10, 11];
2
1
-- [1, 2, 3, 4, 10, 11];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM