JavaScript Array sort() Method

You are Here:

JavaScript Array sort() Method

The sort() method sorts (either numeric or alphabetic) the items of an array.

Note: Numeric value has more priority over alphabetic value.

Note: This method overwrites the original array.

Sorting Alphabetic Values (Ascending Order)

In the following example, we will sort all the elements in an array in ascending order.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myFruit = ["Mango", "Cherry", "Jackfruit", "Apple"]; myFruit.sort(); document.write(myFruit); </script> </body> </html>

Syntax

Array.sort()

Sorting Alphabetic Values (Descending Order)

In the following example, we will sort all the elements in an array in descending order.

Note: First sort the elements in an array and then reverse the array.

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myFruit = ["Mango", "Cherry", "Jackfruit", "Apple"]; // Using sort() and reverse() array function myFruit.sort().reverse(); document.write(myFruit); </script> </body> </html>

Sorting Numeric Values (Ascending Order)

In the following example, we will sort all the elements in an array in ascending order.

Note: While sorting numeric values, only the first digit is considered. If any two elements in an array have the same first digit, then the next digit will be considered.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myNumber = [7, 10, 5, 25]; myNumber.sort() document.write(myNumber); </script> </body> </html>

Sorting Numbers (Ascending Order)

In the following example, we will sort all the numbers in an array in ascending order.

Note: Unlike previous example, this example will consider the whole digit.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myNumber = [7, 10, 5, 25]; myNumber.sort(function(a, b) { return a - b; }); document.write(myNumber); </script> </body> </html>

Sorting Numbers (Descending Order)

In the following example, we will sort all the numbers in an array in descending order.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myNumber = [7, 10, 5, 25]; myNumber.sort(function(a, b) { return a - b; }); //Using reverse() array function myNumber.reverse(); document.write(myNumber); </script> </body> </html>

Lowest Value in Array

In the following example, we will find the lowest value in an array.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>Lowest value in an array is</p> <script> var myNumber = [7, 10, 5, 25]; myNumber.sort(function(a, b) { return a - b; }); document.write(myNumber[0]); </script> </body> </html>

Highest Value in Array

In the following example, we will find the highest value in an array.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>Highest value in an array is</p> <script> var myNumber = [7, 10, 5, 25]; var len = myNumber.length; myNumber.sort(function(a, b) { return a - b; }); document.write(myNumber[len - 1]); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author