按升序对数组排序

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript 数组排序</h1>
  6.  
  7. <p>sort() 方法按字母顺序对数组进行排序。</p>
  8.  
  9. <button onclick="myFunction()">试一试</button>
  10.  
  11. <p id="demo"></p>
  12.  
  13. <script>
  14. var fruits = ["Banana", "Orange", "Apple", "Mango"];
  15. document.getElementById("demo").innerHTML = fruits;
  16.  
  17. function myFunction() {
  18. fruits.sort();
  19. document.getElementById("demo").innerHTML = fruits;
  20. }
  21. </script>
  22.  
  23. </body>
  24. </html>