var arr = [2,10,6,9,7,8]; var arr1 = arr.sort(); var arr2 = arr.sort(function(a,b){ if (a>b){ return -1; }else if(a<b){ return 1 } }); alert(arr1);//10,9,8,7,6,2 alert(arr2);//10,9,8,7,6,2 alert(arr);//10,9,8,7,6,2
问题:为什么arr1不是按照字符的unicode位点进行排序?
答案:感谢网友解答----链接
总结:
arr === arr1 ===arr2; //true
arr.sort返回的是排序后arr的引用,所以当第二次arr.sort后修改,arr的值也发生了改变,所以arr1的值也发生了改变。