终于明白了 Array.sort(comparator) 的原理
原文地址:
https://www.jameskerr.blog/posts/javascript-sort-comparators/
After 13 years of JavaScript, I finally have a way to remember how the comparator function in Array.sort() works.
使用 JavaScript 13 年之后,我终于有办法记住 Array.sort() 中的比较器函数是如何工作的。
I think the trouble is that all the examples use this shorthand syntax.
我认为问题在于所有示例都使用这种缩写语法。
array.sort((a, b) => a - b); // too hard for me
This is beyond confusing for me.
这让我很困惑。
In the past, I would just try b - a then try a - b and pick which one gave me the result I wanted.
在过去,我只是尝试 b - a
然后尝试 a - b
,之后 选择一个给了我我想要的结果的那种写法。
But now I have a mental model simple enough for me to remember.
但现在我有了一个简单易记的心理模型。
First, the sole purpose of the comparator function is to answer this quesions:
首先,比较器函数的唯一目的是回答这个问题:
Where should “a” be placed in the new sorted array? To the left of “b” or to the right?
“a”应该放在新排序数组中的什么位置? 在“b”的左边还是右边?
The arguments passed to the comparator function are usually named a and b.
传递给比较器函数的参数通常命名为a和b。
This makes sense to me, since the first arg comes before the second arg, and a comes before b in the English alphabet.
这对我来说很有意义,因为第一个参数位于第二个参数之前,并且在英语字母表中a位于b之前
These arguments represent two items in the array.
这些参数代表数组中的两个项目。
Now let’s think about the return value.
现在让我们考虑一下返回值。
The function must return a number.
该函数必须返回一个数字。
Numbers exist on a number line going from left to right just like the items in an array.
数字存在于从左到右的数轴上,就像数组中的item项一样。
The negative numbers are on the left, zero is in the middle, and the positive numbers on the right.
负数在左边,零在中间,正数在右边。
-3 -2 -1 0 1 2 3
----------------------------
a good ol' number line
So check it out, if your comparator function returns a negative number, the first argument a will come first, before b.
所以检查一下,如果你的比较器函数返回一个负数,第一个参数a将出现在b之前。
Just like negative numbers on the number line come first!
就像数轴上的负数先出现一样!
If the function returns a positive number, the first argument a will come after b.
如果函数返回正数,则第一个参数a将在b后面。
The a item will be on the “right” side of of b, just like the positive numbers are on the “right” side of the number
line!
该a项目将位于b 的“右侧” ,就像正数位于数轴的“右侧”一样!
If the function returns 0, there will be no change to the existing order of the elements.
如果函数返回 0,则元素的现有顺序不会改变。
This one was pretty easy to remember.
这个很容易记住。
To summarize, we just want to find out where a goes. Does it go to the left or right of b.
总而言之,我们只是想弄清楚a是要放在b的左边还是右边。
Negative means left, positive means right.
负数表示左,正数表示右。
Number line. Left to right.
数轴上 从左到右。
The “left-to-right-ness” of the number line, the items in an array, the alphabet, and the positional arguments all
finally clicked for me today.
从左到右的数轴、数组中的项、字母表和位置参数 这些,今天,终于让我明白了 array.sort(comparator) 是如何工作的。
Maybe this will click for you too and you can save yourself 13 years of googling “How does array.sort(comparator) work again?”.
也许这也会适合你,你可以节省 13 年的时间去谷歌搜索“ array.sort(comparator) 是如何工作的?”。
标签:sort,function,right,comparator,number,Array,array From: https://www.cnblogs.com/longmo666/p/18185415