按降序对数组排序
- <!DOCTYPE html>
- <html>
- <body>
-
- <h1>JavaScript 数组排序反转</h1>
-
- <p>reverse() 方法反转数组中的元素。</p>
-
- <p>通过组合 sort() 和 reverse(),您可以按降序对数组进行排序。</p>
-
- <button onclick="myFunction()">试一试</button>
-
- <p id="demo"></p>
-
- <script>
- // Create and display an array:
- var fruits = ["Banana", "Orange", "Apple", "Mango"];
- document.getElementById("demo").innerHTML = fruits;
-
- function myFunction() {
- // First sort the array
- fruits.sort();
- // Then reverse it:
- fruits.reverse();
- document.getElementById("demo").innerHTML = fruits;
- }
- </script>
-
- </body>
- </html>