- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Array sort
JavaScript Arrays
JavaScript Array sort
JavaScript
sortarranges the elements of an array and changes the original.
By default it sorts as text, which gives surprising results for numbers.
A compare function fixes that.
Example
Example
javascript
const numbers = [10, 1, 5];
console.log(numbers.sort().join(","));The output is 1,10,5, not 1,5,10.
This is because the values were compared as text.
