数组排序 sort (原数组被修改)
1 var arr1 = ['a','c','d','b']; 2 console.log(arr1.sort()); // 返回 ['a','b','c','d']; 3 console.log(arr1); // 返回 ['a','b','c','d']; 原数组被修改 4 var arr2 = [41,23,50,3]; 5 console.log(arr2.sort()); // 23, 3, 41, 50 6 7 arr2.sort(function(a, b) { 8 return a-b; // 升序 9 // return b-a; //降序 10 }); 11 console.log(arr2);
// 合并数组并且从小到大排列 function sortMixArr(arr1, arr2) { var arr = arr1.concat(arr2); return arr.sort(function(a,b) { return a-b; }); } var arr1 = [1,3,2]; var arr2 = [3,5,4]; console.log(sortMixArr(arr1, arr2)); // 1,2,3,4,5,6