<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sort and Reverse Array Methods</title> </head> <body> <h1>JavaScript Array Sorting and Reversing</h1> <script> const fruits = ["Banana", "Apple", "Orange", "Mango"]; console.log("Original Fruits Array:", fruits); fruits.sort(); console.log("Sorted Fruits Array:", fruits); const numbers = [10, 2, 5, 30]; console.log("\nOriginal Numbers Array:", numbers); numbers.sort((a, b) => a - b); console.log("Sorted Numbers (Ascending):", numbers); numbers.sort((a, b) => b - a); console.log("Sorted Numbers (Descending):", numbers); const letters = ["a", "b", "c", "d"]; console.log("\nOriginal Letters Array:", letters); letters.reverse(); console.log("Reversed Letters Array:", letters); const mixNumbers = [15, 3, 25, 8]; mixNumbers.sort((a, b) => a - b).reverse(); console.log("\nMixed Numbers Sorted in Descending Order:", mixNumbers); // Output: [25, 15, 8, 3] </script> </body> </html>
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter