运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
转到 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Arrays</h1> <h2>Sort in Reverse</h2> <p>The reverse() method reverses the elements in an array.</p> <p>By combining sort() and reverse() you can sort an array in descending order:</p> <p id="demo1"></p> <p id="demo2"></p> <script> // Create and display an array: const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; // First sort the array fruits.sort(); // Then reverse it: fruits.reverse(); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>