Why does points.sort(function(a, b){return a-b}); return -1, 0 or 1?

前端 未结 6 1621
滥情空心
滥情空心 2020-12-08 23:47

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 w

相关标签:
6条回答
  • 2020-12-09 00:20

    The sort callback has to return

    • a negative number if a < b
    • 0 if a === b
    • a positive number if a > b

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

    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

    • if a < b then a - b < 0, i.e. a negative number
    • if a === b then a - b === 0, i.e. 0
    • if a > b then a - b > 0, i.e. a positive number

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

    0 讨论(0)
  • 2020-12-09 00:28

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

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

    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.

    0 讨论(0)
  • 2020-12-09 00:34

    I am trying to answer your "It does indeed work BUT Why?"

    Here is the syntax of the sort method in javascript. Note that the compareFunction is optional.

    array.sort([compareFunction]);
    

    When compareFunction is not provided:

    When compareFunction is not provided, the default mode of sorting is followed wherein the elements of the array are converted into strings and than compared according to the UTF 16 code unit values and the elements are sorted in ascending order by default.

    e.g. For string values, it follows simple UTF 16 code order. like "Banana" appears before "Cat"

    for numbers, they first gets converted into strings and then compared according to UTF 16 code oder. So, 80 and 9 gets converted to strings "80" and "9" respectively and "80" comes before "9" according to the UTF 16 order.

    When compareFunction is provided:

    When compareFunction is provided, then the function takes over the sorting process and all the non-undefined elements gets sorted based on the return value of compareFunction according to the following logic.

    If the compareFunction(a,b) returns negative value, a comes first by getting lower index than b.

    If compareFunction(1,b) returns zero then both get the same index and hence do not change place respect to each other but both gets sorted with all other elements.

    If compareFunction(a,b) returns positive value, a gets higher index than b and hence a comes after b.

    The example you provided, uses a trick to decide each numbers comparative weightage in terms of its numeric value by subtracting each pair of numbers from the array.

    0 讨论(0)
  • 2020-12-09 00:36

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

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

    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.

    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.

    0 讨论(0)
  • 2020-12-09 00:41

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

    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.

    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:

    a.sort(function() {return (a%10) - (b%10);}
    
    0 讨论(0)
  • 2020-12-09 00:43

    Quoting from ECMA Script Reference:

    Array.prototype.sort (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.

    The rest are implementation details that could differ between interpreters.

    0 讨论(0)
提交回复
热议问题