| chapter | 6 |
|---|---|
| pageNumber | 57 |
| description | The sort method sorts the items of an array in specific order. By default, it sorts the elements as strings and arranges them in ascending order based on their UTF-16 code unit values. |
The sort method sorts the items of an array in a specific order (ascending or descending). By default, the sort method sorts the elements as strings and arranges them in ascending order based on their UTF-16 code unit values.
Here's the syntax for using sort:
array.sort([compareFunction]);The compareFunction argument is optional and specifies a function that defines the sort order. If omitted, the elements are sorted in ascending order according to their string representation.
For example:
let city = ["California", "Barcelona", "Paris", "Kathmandu"];
let sortedCity = city.sort();
console.log(sortedCity);
// Result: ['Barcelona', 'California', 'Kathmandu', 'Paris']{% hint style="info" %} Numbers can be sorted incorrectly when they are sorted. For example, "35" is bigger than "100", because "3" is bigger than "1". {% endhint %}
To fix the sorting issue in numbers, compare functions are used. Compare functions defines sort orders and return a negative, zero, or positive value based on arguments, like this:
- A negative value if
ashould be sorted beforeb - A positive value if
ashould be sorted afterb 0ifaandbare equal and their order doesn't matter
const points = [40, 100, 1, 5, 25, 10];
points.sort((a, b) => {return a-b});
// Result: [1, 5, 10, 25, 40, 100]{% hint style="warning" %}
The sort() method overrides the original array.
{% endhint %}