函數作為參數使用 var arr = [1, 100, 20, 200, 40, 50, 120, 10]; //排序 arr.sort(); console.log(arr); 排序 函數作為參數使用, 匿名函數作為sort方法的參數使用, 那麼此時的匿名函數中有兩個參數 var arr = [1 ...
函數作為參數使用
var arr = [1, 100, 20, 200, 40, 50, 120, 10]; //排序 arr.sort(); console.log(arr);
排序---函數作為參數使用, 匿名函數作為sort方法的參數使用, 那麼此時的匿名函數中有兩個參數
var arr = [1, 100, 20, 200, 40, 50, 120, 10]; //排序---函數作為參數使用,匿名函數作為sort方法的參數使用,那麼此時的匿名函數中有兩個參數, arr.sort(function (obj1, obj2) { if (obj1 > obj2) { return -1; } else if (obj1 == obj2) { return 0; } else { return 1; } }); console.log(arr);
var arr1 = ["acdef", "abcd", "bcedf", "bced"]; arr1.sort(function (a, b) { if (a > b) { return 1; } else if (a == b) { return 0; } else { return -1; } }); console.log(arr1);