按降序排列数字

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript 数组排序</h1>
  6.  
  7. <p>单击按钮可按降序对数组进行排序。</p>
  8.  
  9. <button onclick="myFunction()">试一试</button>
  10.  
  11. <p id="demo"></p>
  12.  
  13. <script>
  14. var points = [40, 100, 1, 5, 25, 10];
  15. document.getElementById("demo").innerHTML = points;
  16.  
  17. function myFunction() {
  18. points.sort(function(a, b){return b - a});
  19. document.getElementById("demo").innerHTML = points;
  20. }
  21. </script>
  22.  
  23. </body>
  24. </html>