排序数字(按字母顺序或数字顺序)
- <!DOCTYPE html>
- <html>
- <body>
-
- <h1>JavaScript 数组排序</h1>
-
- <p>单击按钮可按字母顺序或数字顺序对数组进行排序。</p>
-
- <button onclick="myFunction1()">按字母排序</button>
- <button onclick="myFunction2()">按数字排序</button>
-
- <p id="demo"></p>
-
- <script>
- var points = [40, 100, 1, 5, 25, 10];
- document.getElementById("demo").innerHTML = points;
-
- function myFunction1() {
- points.sort();
- document.getElementById("demo").innerHTML = points;
- }
- function myFunction2() {
- points.sort(function(a, b){return a - b});
- document.getElementById("demo").innerHTML = points;
- }
- </script>
-
- </body>
- </html>