Javascript Sorting And Reversing An Array

JS Code Snippets Sorting And Reversing An Array
Share:

About

In this code snippet, we’ll learn how to sort and reverse arrays in Javascript.

Let’s see the code example below.

Code:

let numbers = [ 3, 8, 7, 1 ];
let characters = [ "c", "s", "a", "n" ];
let strings = [ "car", "truck", "bike" ];

//Sort////////////////////////////////////////////////////

numbers.sort(); //Will sort array numerically.  
console.log(numbers);


characters.sort(); //Will sort array alphabetically.  
strings.sort(); //Will sort array alphabetically.  
console.log(characters);
console.log(strings);

/////////////////////////////////////////////////////////
 
 
//Reverse////////////////////////////////////////////////

numbers.reverse(); //Will reverse elements in the array.  
console.log(numbers);

/////////////////////////////////////////////////////////

Resulting output:

Share:

Leave a Reply

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

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger