JavaScript Array sort() Method
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
Syntax
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.
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
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
Sorting Numbers (Descending Order)
In the following example, we will sort all the numbers in an array in descending order.
Example
Lowest Value in Array
In the following example, we will find the lowest value in an array.
Example
Highest Value in Array
In the following example, we will find the highest value in an array.
Example
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.