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

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

提交回复
热议问题