JavaScript Array sort()
- Previous Page some()
- Next Page splice()
- Go to the Previous Level JavaScript Array Reference Manual
Definition and Usage
sort()
The method sorts the items in the array.
The sorting order can be alphabetical or numerical, as well as ascending (up) or descending (down).
By default,sort()
The method will sort values as strings in alphabetical and ascending order.
This applies to strings ("Apple" appears before "Banana"). However, if numbers are sorted as strings, "25" is greater than "100" because "2" is greater than "1".
Therefore,sort()
The method may produce incorrect results when sorting numbers.
You can solve this problem by providing a 'comparison function' (see the 'Parameter Values' below).
Note:sort()
The method will change the original array.
Instance
Example 1
Sort the array:
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort();
Example 2
Sort numbers in ascending order in the array:
var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a-b});
Example 3
Sort numbers in descending order in the array:
var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return b-a});
Example 4
Get the minimum value in the array:
var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a-b}); // Sort the numbers in the array in ascending order // The first item (points[0]) in the array is now the lowest value
Example 5
Get the maximum value in the array:
var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return b-a}); // Sort the numbers in the array in descending order // The first item (points[0]) in the array is now the highest value
Example 6
Get the maximum value in the array:
const points = [40, 100, 1, 5, 25, 10]; // Sort the numbers in ascending order: points.sort(function(a, b){return a-b}); // points[points.length-1] = 100 (highest value)
Example 7
Sort the array in alphabetical order and then reverse the order of the sorted items (descending):
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); fruits.reverse();
Syntax
array.sort(compareFunction)
Parameter Values
Parameters | Description |
---|---|
compareFunction |
Optional. Defines a function to replace the sorting order. This function should return a negative value, zero value, or positive value depending on the parameters, for example:
When the sort() method compares two values, it sends the values to the comparison function and sorts the values based on the returned (negative, zero, positive) value. Example:When comparing 40 and 100, the sort() method calls the comparison function (40, 100). This function calculates 40-100 and returns -60 (negative value). The sort function will sort 40 as a value less than 100. |
Technical Details
Return Value: | Array object, with items sorted. |
---|---|
JavaScript Version: | ECMAScript 1 |
Browser Support
All browsers fully support sort()
Methods:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
Related Pages
Tutorial:JavaScript Array
Tutorial:JavaScript Array Const
Tutorial:JavaScript Array Methods
Tutorial:JavaScript Array Sorting
Tutorial:JavaScript Array Iteration
- Previous Page some()
- Next Page splice()
- Go to the Previous Level JavaScript Array Reference Manual