问答详情
源自:7-22 数组排序sort()

我换成BUTTON 哪里出错了 运行不了

<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Array对象 </title>
   <script type="text/javascript">
       function sortNum(a,b) {
           var myarr = new Array("80","16","50","6","100","1");
           return b - a;
           var soult = myarr.sort(sortNum);
       }
       document.getElementById("demo").innerHTML=soult;
   </script>
</head>
<body>
<button onclick="sortNum(a,b)">点我对函数排序</button>
<p id="demo">80,16,50,6,100,1</p>
</body>
</html>

提问者:慕移动1741139 2017-08-15 09:55

个回答

  • 慕粉4395516
    2017-08-15 11:09:01
    已采纳

    function sortNum() {

               var soult;

               var myarr = new Array("80","16","50","6","100","1");

               soult = myarr.sort(function compare(a,b){return a -b;});

               document.getElementById("demo").innerHTML=soult;

           }


  • 慕圣9424722
    2017-08-19 00:47:55

    <!DOCTYPE html>

    <html>

    <head>

       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

       <title>Array对象 </title>

       <script type="text/javascript">

          function b(){

          var myarr = new Array("80","16","50","6","100","1");

          function sortNum(a,b) {

               return b - a;  //升序

           }

           var soult = myarr.sort(sortNum);

           document.getElementById("demo").innerHTML=soult;

          }

       </script>

    </head>

    <body>

    <button onclick="b()">点我对函数排序</button>

    <p id="demo">80,16,50,6,100,1</p>

    </body>

    </html>