运行 ❯
获取您的
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
转到空间
<!DOCTYPE html> <html> <body> <h1>JavaScript Arrays</h1> <h2>The sort() Method</h2> <p>Click the buttons to sort the array alphabetically or numerically.</p> <button onclick="myFunction1()">Sort Alphabetically</button> <button onclick="myFunction2()">Sort Numerically</button> <p id="demo"></p> <script> const 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>