js數組排序之sort:用Math.random打亂數組。


<script>
var points = [40, 100, 1, 5, 25, 10]; 

points.sort(function(a, b){    //function(a, b) 是一個排序指針方法

return 0.5 - Math.random()   //  a -b 從小到大, b - a 從大到小 ,Math.random()  產生一個隨機數,大於0.5 數組從小到大,小於0.5,數組從大到小排。

});
</script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM