js sort()
定义和用法
sort()用于对数组的元素进行排序
例子:
正序排列
<script> var arr=[1,5,3,4,6,7,1,9,5,3]; var newarr=[]; for(i=0;i<arr.length;i++){ if(newarr.indexOf(arr[i])==-1){ newarr.push(arr[i]) } } newarr.sort(); console.log(newarr)//[1, 3, 4, 5, 6, 7, 9] </script>
倒序排列
<script> var arr=[1,5,3,4,6,7,1,9,5,3]; var newarr=[]; for(i=0;i<arr.length;i++){ if(newarr.indexOf(arr[i])==-1){ newarr.push(arr[i]) } } newarr.sort(function pl(a,b){return b-a;}); console.log(newarr)//[9, 7, 6, 5, 4, 3, 1] </script>
如有错误 请指出 谢谢!