Home  >  Article  >  Web Front-end  >  How to sort an out-of-order array in js

How to sort an out-of-order array in js

王林
王林forward
2020-05-14 09:18:382102browse

How to sort an out-of-order array in js

The specific code is:

<!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

Recommended tutorial: js entry tutorial

The above is the detailed content of How to sort an out-of-order array in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete