首頁  >  文章  >  web前端  >  js如何對亂序數組進行排序

js如何對亂序數組進行排序

王林
王林轉載
2020-05-14 09:18:382058瀏覽

js如何對亂序數組進行排序

具體程式碼為:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        arr.sort(function (a, b) {
            return Math.random() - 0.5; //使用随机数,大于0.5的往后排,小于0.5的往前排
        })
        console.log(arr);
    </script>
</body>

</html>

index.html

推薦教學:js入門教學

以上是js如何對亂序數組進行排序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除