Array .sort() / reverse()
Methods for sorting and reversing array elements. sort() lets you control the order with a comparison function, and reverse() reverses the order of elements.
Syntax
// Sorts the array. The original array is modified.
array.sort();
array.sort(function(a, b) { return comparisonResult; });
// Reverses the array. The original array is modified.
array.reverse();
// Returns a new sorted array without modifying the original.
var newArray = array.toSorted(function(a, b) { return comparisonResult; });
// Returns a new reversed array without modifying the original.
var newArray = array.toReversed();
Method List
| Method | Description |
|---|---|
| sort(compareFunction) | Sorts the elements of the array. If the comparison function is omitted, elements are sorted in Unicode order. The original array is modified. |
| reverse() | Reverses the order of the elements in the array. The original array is modified. |
| toSorted(compareFunction) | Same as sort(), but returns a new array without modifying the original. Added in ES2023. |
| toReversed() | Same as reverse(), but returns a new array without modifying the original. Added in ES2023. |
Sample Code
// Sorting strings
var fruits = ["Banana", "Apple", "Orange"];
fruits.sort();
console.log(fruits); // Outputs 'Apple,Banana,Orange' — sorted in Unicode order.
// Sorting numbers requires a comparison function.
var numbers = [10, 1, 21, 2];
numbers.sort(function(a, b) {
return a - b; // Ascending order
});
console.log(numbers); // Outputs '1,2,10,21'.
// Sorting in descending order
numbers.sort(function(a, b) {
return b - a;
});
console.log(numbers); // Outputs '21,10,2,1'.
// Reversing an array.
var letters = ["A", "B", "C", "D"];
letters.reverse();
console.log(letters); // Outputs 'D,C,B,A'.
// Using toSorted() to preserve the original array.
var original = [3, 1, 4, 1, 5];
var sorted = original.toSorted(function(a, b) {
return a - b;
});
console.log(sorted); // Outputs '1,1,3,4,5'.
console.log(original); // Outputs '3,1,4,1,5' — the original array is unchanged.
Notes
When you call array.sort() without a comparison function, elements are converted to strings and sorted in Unicode order. For numeric arrays, this means [10, 1, 21, 2] would be sorted as [1, 10, 2, 21] — as strings rather than numbers. To sort numbers correctly, you need a comparison function such as function(a, b) { return a - b; }.
The comparison function takes two arguments. If the return value is negative, the first argument comes first; if positive, the second argument comes first; if zero, the order is unchanged. Returning a - b gives ascending order and b - a gives descending order — this pattern also works for sorting arrays of objects by a specific property.
array.sort() and array.reverse() are destructive methods that modify the original array directly. If you need to preserve the original, use toSorted() and toReversed() (added in ES2023), or copy the array with array.slice() before sorting.
Browser Compatibility
4.5 or earlier ×
3 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.