function compare(a, b) {
if (a is less than b by some ordering criterion) {
// how minus 1 will affect to sort? how sort will understand this value?
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0;
}
或在下面的例子中是如何影響:我無法理解「迴歸」價值排序
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers);
// [1, 2, 3, 4, 5]
我知道只知道「真」或「假」,這是1和0。如何比較和排序將明白什麼是(1,-1,0),以及如何使用這些值進行排列?
重複的http://stackoverflow.com/questions/6567941/how-does-sort-function-work-in-javascript-along-with-compare-function – yakobom
可能的重複[如何排序函數在JavaScript中工作,以及比較函數](http://stackoverflow.com/questions/6567941/how-does-sort-function-work-in-javascript-along-with-compare-function) – SOFe