JavaScript
JavaScript Array Type
Sort
The sort method sorts an array in-place (mutates it). We can use the
default compare function for most purposes, but we can also supply a
custom compare function that decides how to sort the array.
Custom compare functions must take two parameters which are two elements from the array. It must then return a number which tells the sort function the compartive relationship between those two elements:
- Less than zero: Element from first parameter comes first.
- Zero: Elements are equal.
- Greater than zero: Element from second parameter comes first.
Code example
const arr = [12, 32, 1, 43]const sorted = arr.sort()console.log(sorted) // [1, 12, 32, 43]
The default sorting order is ascending.
Read more on MDN
Array.prototype.sort()