1. 程式人生 > >js實現升序和降序

js實現升序和降序

升序 document code htm cti ret type doctype itl

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <script>
            function sortUp(a,b){
                return a-b;
            }
            function sortDown(a,b){
                return b-a;
            }
            var arr=new Array(6);
            arr[0]=10;
            arr[1]=5;
            arr[2]=8;
            arr[3]=30;
            arr[4]=55;
            arr[5]=32;
            // 升序
            document.write(arr.sort(sortUp));
            // 5,8,10,30,32,55
            //降序
            document.write(arr.sort(sortDown));
            // 55,32,30,10,8,5
        </script>
    </body>
</html>

js實現升序和降序