-2
我知道排序數組,但我有我想知道的問題。arrayObj.sort的說明([sortFunction])
例如:
var array = [9,3,0,-2,15];
那麼,讓我們對其進行排序:
function sortFunction(a, b){
if(a < b) return -1; // or any number that less than zero, but why?
if(a > b) return 1; // or any number that above zero, but why?
if(a = b) return 0; // but why?
// I know it may be easier a - b, I've written it for clarity
}
array.sort(sortFunction); //so we'll get correct result
如何理解爲什麼它返回-1,1和0排序數組?
P.S.對不起,如果問題似乎很愚蠢,我還沒有在Google中找到答案。
閱讀此https://stackoverflow.com/questions/234683/javascript-array-sort-implementation –
閱讀[文檔](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description),它解釋得很清楚 – George
我被誤解了( – Jarvis