数组的高级API-sort

按升降序排列数组项。
本身存在的问题:只能通过第一位排列。
解决方法:通过回调函数进行规制设置。

a - b 升序。

b - a 降续。

其内部运用了冒泡排序

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=script, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        var arr = [100,10,8,-10,1000];

       console.log( arr.sort(function (a,b){
            return a - b;
        }));

        console.log( arr.sort(function (a,b){
            return b - a;
        }));


    </script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/-Tony/p/9220370.html
今日推荐