Sort
In JavaScript, the .sort()
method of an array sorts the contents of an array and returns the sorted array. This sorting is done in place and affects the original array. No copy is made. The default sort is in ascending string order.
The .sort()
method allows the passing of a comparison function to change the ordering of the sort.
Syntax
// Perform the default sort
someArray.sort()
// Perform the sort using an arrow function for comparisons
somearray.sort((A, B) => { ... } )
// Perform the sort with an inline compare function
somearray.sort(function compareFn(A, B) { ... })
// Perform the sort with a compare function
somearray.sort(compareFn)
compareFn
is the optional comparison function.A
is the first array item being compared.B
is the second array item being compared.
Default Sort Order
If no comparison function are provided, the .sort()
method will sort the array in ascending string order.
For items that are not strings, .sort()
will convert them into strings before comparing them. This can lead to unexpected results:
let numbers = [33, 16, 156, 2, 9, 5, 10];numbers.sort();console.log(numbers);// Output: [10, 156, 16, 2, 33, 5, 9]
Comparison Function
The comparison function, if provided, will determine the sorting of all non-undefined
items in the array. All undefined
items are sorted to the end of the array, and no undefined
items are passed to the comparison function.
The comparison function determines the sort order as follows:
For the function CompareFn(A, B)
:
- If the function returns a value greater than zero, sort
B
beforeA
. - If the function returns a value less than zero, sort
A
beforeB
. - If the function returns a value of zero, the positions of
A
andB
remain unchanged. - The function must return the same result for any specific pair of values
A
andB
provided. Otherwise, the sort order is undefined.
To sort an array in numeric order rather than string order, the following function can be used as long as the array doesn’t contain Infinity
or NaN
:
function compareFn(A, B) {return A - B;}
So we can fix the prior example:
let numbers = [33, 16, 156, 2, 9, 5, 10];numbers.sort(function compareFn(A, B) {return A - B;});console.log(numbers);// Output: [2, 5, 9, 10, 16, 33, 156]
Codebyte Example
The below Codebyte example uses a comparison function with .sort()
in order to sort the array alphabetically while ignoring the case:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.