排序数字(按字母顺序或数字顺序)

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