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

前端 未结 6 1681
滥情空心
滥情空心 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: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.

提交回复
热议问题