为什么点。排序(函数(a,b){返回a - b });返回-1 0还是1?

时间:2022-10-13 17:01:59

My difficulty here could be my mathematical illiteracy, but I was trying to sort some numbers in a JavaScript array and this is the solution I found online. It does indeed work, but my question is why?! I would really like to understand this piece of code properly.

我的困难可能是数学上的文盲,但我试着在一个JavaScript数组中对一些数字进行排序,这是我在网上找到的解决方案。它确实有效,但我的问题是为什么?!我很想正确地理解这段代码。

The site, W3 Schools says:

W3学校表示:

You can fix this by providing a function that returns -1, 0, or 1:

你可以通过提供一个返回-1、0或1的函数来解决这个问题:

var points = [40, 100, 1, 5, 25, 10];

points.sort(function(a,b){return a-b});

Why would only -1, 0 or 1 be returned? I have Googled, and return can return pretty much any value you want.

为什么只返回-1、0或1 ?我已经用谷歌搜索过了,而且还可以返回任何你想要的值。

Again, if this is an incredibly dumb question I apologise.

如果这是一个非常愚蠢的问题,我道歉。

5 个解决方案

#1


8  

The sort callback has to return

排序回调必须返回。

  • a negative number if a < b
  • 一个负数,如果a < b。
  • 0 if a === b
  • 如果a == b。
  • a positive number if a > b
  • 一个正数,如果是> b。

Three possible return values are needed because the sort function needs to now whether a is smaller than, equal to, or larger than b in order to correctly position a in the result array.

需要三种可能的返回值,因为排序函数需要现在是否小于、等于或大于b,以便在结果数组中正确地定位a。

It is very common to just return -1, 0 and 1 if you working with non-numerical data (I guess that's why W3Schools mentions it). But if you use numerical data, you can simply subtract the values because

如果使用非数值数据(我想这就是W3Schools提到的原因),返回-1、0和1是很常见的。但是如果你使用数值数据,你可以简单地减去这些值。

  • if a < b then a - b < 0, i.e. a negative number
  • 如果a < b,那么a - b < 0,即一个负数。
  • if a === b then a - b === 0, i.e. 0
  • 如果a == b那么a - b == 0,即0。
  • if a > b then a - b > 0, i.e. a positive number
  • 如果> b,那么a - b > 0,即一个正数。

W3Schools is not very precise which is one of the reason why you should avoid it. Use MDN instead.

w3学校不是很精确,这是你应该避免它的原因之一。使用MDN相反。

#2


4  

It is not bound to be -1, 0, 1 any negative or positive number will do the trick.

它不等于-1 0 1任何负数或正数都可以。

But how it works? The sort() method needs to know the relation between each two elements in order to sort them. For each pair (according to the algorithm used) it calls the function then based on the return value, may swap them. Note that a-b returns a negative value if b>a and a positive one if a>b. That leads to an ascending order.

但它是如何工作的呢?sort()方法需要知道每两个元素之间的关系,以便对它们进行排序。对于每一对(根据所使用的算法),它根据返回值调用函数,可以交换它们。注意,如果b>a和>b都是正的,a-b返回一个负值。这将导致一个升序。

Just for test, replace a-b with b-a and you will get a descending order. You may even make it more complicated, sort them based on (for example) their least significant digit:

只是为了测试,用b-a替换a-b,你会得到一个降序。你甚至可以让它变得更复杂,根据(例如)它们最不重要的数字来排序:

a.sort(function() {return (a%10) - (b%10);}

#3


1  

See the specification, http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4.14

看到规范,http://www.ecma-international.org/ecma-262/5.1/ sec-15.5.4.14

Don't rely on things that w3schools has to say.

不要依赖w3schools所说的东西。

If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative value if x < y, zero if x = y, or a positive value if x > y.

如果comparefn不是未定义的,那么它应该是一个接受两个参数x和y的函数,如果x < y,如果x = y,或者x > y为正值,则返回一个负值。

#4


1  

Quoting from ECMA Script Reference:

引用ECMA脚本参考:

Array.prototype.sort (comparefn)

Array.prototype。排序(comparefn)

The elements of this array are sorted. The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order). If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative value if x < y, zero if x = y, or a positive value if x > y.

该数组的元素已排序。排序不一定是稳定的(也就是说,比较相等的元素不一定保持原来的顺序)。如果comparefn不是未定义的,那么它应该是一个接受两个参数x和y的函数,如果x < y,如果x = y,或者x > y为正值,则返回一个负值。

The rest are implementation details that could differ between interpreters.

其余的是可以在解释器之间不同的实现细节。

#5


1  

If Function(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.

如果函数(a, b)小于0,将a排序为低于b的指数,即a为第一。

If Function(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

如果函数(a, b)返回0,将a和b保持不变,但对所有不同元素进行排序。

Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.

注意:ECMAscript标准不能保证这种行为,因此并不是所有的浏览器(例如至少2003年的Mozilla版本)都尊重这一点。

If Function(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first. Function(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.

如果函数(a, b)大于0,将b排序为低于a的指数,即b优先。函数(a, b)在给定一对元素a和b作为其两个参数时,必须始终返回相同的值。如果返回不一致的结果,则排序顺序没有定义。

#1


8  

The sort callback has to return

排序回调必须返回。

  • a negative number if a < b
  • 一个负数,如果a < b。
  • 0 if a === b
  • 如果a == b。
  • a positive number if a > b
  • 一个正数,如果是> b。

Three possible return values are needed because the sort function needs to now whether a is smaller than, equal to, or larger than b in order to correctly position a in the result array.

需要三种可能的返回值,因为排序函数需要现在是否小于、等于或大于b,以便在结果数组中正确地定位a。

It is very common to just return -1, 0 and 1 if you working with non-numerical data (I guess that's why W3Schools mentions it). But if you use numerical data, you can simply subtract the values because

如果使用非数值数据(我想这就是W3Schools提到的原因),返回-1、0和1是很常见的。但是如果你使用数值数据,你可以简单地减去这些值。

  • if a < b then a - b < 0, i.e. a negative number
  • 如果a < b,那么a - b < 0,即一个负数。
  • if a === b then a - b === 0, i.e. 0
  • 如果a == b那么a - b == 0,即0。
  • if a > b then a - b > 0, i.e. a positive number
  • 如果> b,那么a - b > 0,即一个正数。

W3Schools is not very precise which is one of the reason why you should avoid it. Use MDN instead.

w3学校不是很精确,这是你应该避免它的原因之一。使用MDN相反。

#2


4  

It is not bound to be -1, 0, 1 any negative or positive number will do the trick.

它不等于-1 0 1任何负数或正数都可以。

But how it works? The sort() method needs to know the relation between each two elements in order to sort them. For each pair (according to the algorithm used) it calls the function then based on the return value, may swap them. Note that a-b returns a negative value if b>a and a positive one if a>b. That leads to an ascending order.

但它是如何工作的呢?sort()方法需要知道每两个元素之间的关系,以便对它们进行排序。对于每一对(根据所使用的算法),它根据返回值调用函数,可以交换它们。注意,如果b>a和>b都是正的,a-b返回一个负值。这将导致一个升序。

Just for test, replace a-b with b-a and you will get a descending order. You may even make it more complicated, sort them based on (for example) their least significant digit:

只是为了测试,用b-a替换a-b,你会得到一个降序。你甚至可以让它变得更复杂,根据(例如)它们最不重要的数字来排序:

a.sort(function() {return (a%10) - (b%10);}

#3


1  

See the specification, http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4.14

看到规范,http://www.ecma-international.org/ecma-262/5.1/ sec-15.5.4.14

Don't rely on things that w3schools has to say.

不要依赖w3schools所说的东西。

If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative value if x < y, zero if x = y, or a positive value if x > y.

如果comparefn不是未定义的,那么它应该是一个接受两个参数x和y的函数,如果x < y,如果x = y,或者x > y为正值,则返回一个负值。

#4


1  

Quoting from ECMA Script Reference:

引用ECMA脚本参考:

Array.prototype.sort (comparefn)

Array.prototype。排序(comparefn)

The elements of this array are sorted. The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order). If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative value if x < y, zero if x = y, or a positive value if x > y.

该数组的元素已排序。排序不一定是稳定的(也就是说,比较相等的元素不一定保持原来的顺序)。如果comparefn不是未定义的,那么它应该是一个接受两个参数x和y的函数,如果x < y,如果x = y,或者x > y为正值,则返回一个负值。

The rest are implementation details that could differ between interpreters.

其余的是可以在解释器之间不同的实现细节。

#5


1  

If Function(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.

如果函数(a, b)小于0,将a排序为低于b的指数,即a为第一。

If Function(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

如果函数(a, b)返回0,将a和b保持不变,但对所有不同元素进行排序。

Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.

注意:ECMAscript标准不能保证这种行为,因此并不是所有的浏览器(例如至少2003年的Mozilla版本)都尊重这一点。

If Function(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first. Function(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.

如果函数(a, b)大于0,将b排序为低于a的指数,即b优先。函数(a, b)在给定一对元素a和b作为其两个参数时,必须始终返回相同的值。如果返回不一致的结果,则排序顺序没有定义。