sort
By default, sort sorts the elements of an array by first converting them to strings and then applying string comparison (see Concept Comparison). The sorting happens in-place which means the original array is modified. sort also returns that modified array which is convenient if you want to chain other methods to it.
const arr = ['c', 'a', 'z', 'b'];
const result = arr.sort();
console.log(result);
// => ['a', 'b', 'c', 'z']
console.log(arr);
// => ['a', 'b', 'c', 'z']
To customize the sorting behavior, you can pass a comparison function as an argument. The comparison function itself is called with two arguments which are two elements of the array. It then needs to return the following:
- a negative number if the first argument should be sorted before the second
- a positive number if the first argument should be sorted after the second
0if the order of the elements should stay the same
For example, to sort numbers the following comparison function can be used.
const arr = [3, 1, 2, 10];
arr.sort((a, b) => a - b);
// => [1, 2, 3, 10]
// "a - b" is negative when b is greater than a, positive when
// a is greater than b and 0 when they are equal.
Backlinks