The sort() method

sorts an array alphabetically:

Example


var fruits = ["Banana", "Orange", "Apple", "Mango"];

console.log(fruits); 

// [ 'Banana', 'Orange', 'Apple', 'Mango' ]

console.log(fruits.sort());

// [ 'Apple', 'Banana', 'Mango', 'Orange' ]

The reverse() method

Just revrses the array:


var fruits = ["Banana", "Orange", "Apple", "Mango"];

console.log(fruits);

// [ 'Banana', 'Orange', 'Apple', 'Mango' ]

console.log(fruits.reverse());

// [ 'Mango', 'Apple', 'Orange', 'Banana' ]

Order alphabetically in descending order


var fruits = ["Banana", "Orange", "Apple", "Mango"];

console.log(fruits);

// [ 'Banana', 'Orange', 'Apple', 'Mango' ]

console.log(fruits.sort());

// [ 'Apple', 'Banana', 'Mango', 'Orange' ]

console.log(fruits.reverse());

// [ 'Mango', 'Apple', 'Orange', 'Banana' ]

0 Comments

Leave a reply

Your email address will not be published. required fields are marked *