sort()

ES3+

Sorts the elements of an array in place and returns the sorted array.

Syntax

array.sort(compareFunction)

Parameters

compareFunction Function optional

Function that defines the sort order

Return Value

Array

The sorted array

Examples

JavaScript
const numbers = [3, 1, 4, 1, 5, 9];
numbers.sort((a, b) => a - b);
console.log(numbers);
Output:
// [1, 1, 3, 4, 5, 9]

Related Methods