YogeshChauhan.com
Sorting Arrays in JavaScript
August 12, 2020

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

This one just reverses 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' ]
dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 5 How to create flickering text using CSS? Mar 4 How to use data-* Attributes in HTML? Mar 4 The substr() method in JavaScript and how it’s different from substring() Mar 4 A complete guide to add responsive YouTube videos using HTML and CSS Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress?

You might also like these

How to import MySQL small sample database into phpMySQL?SQL/MySQLHow to create a horizontal scrolling menu with and without flex using CSS (or SCSS)?CSSThe Difference Between isNaN() Method And isNaN() Function In JavaScriptJavaScriptContainers 101: What are containers?MiscJavaScript arrays: a separate data type or Objects?JavaScriptKubernetes vs. Docker? It’s a misleading phraseMiscHow to use a Subquery to Insert Multiple Rows in SQL Table?SQL/MySQLLearn to Make a Simple Contact Us Form using PHP and PDO-MySQLPHPHow to change CSS with JavaScript?CSSPHP arrays for US states’ full names and abbreviationsPHPHow to show confirmation alerts with OK and cancel buttons using Swift 5?SwiftHow to get start index and end index (as int) of substring in Swift?Swift