文章地址 https://www.cnblogs.com/sandraryan/
JS自己有sort可以用来排序,可以排string会转为ASCII比较,但是,ASCII对数字的排序不合理
<script> // var arr = ["s","Y","a","A","o","P"]; // console.log(arr.sort()); // 可以修改sort排序,以便于为数字进行快速排序 var arr2 = [12,3,56,78,45,24]; // console.log(arr2.sort()); //[12, 24, 3, 45, 56, 78] arr2.sort(function(a,b){ return a- b; }); // 升序排列 console.log(arr2); // [3, 12, 24, 45, 56, 78] arr2.sort(function(a,b){ return b-a; }); console.log(arr2); // 降序排列 //[78, 56, 45, 24, 12, 3] </script>