Standard built-ins

Array.prototype.sort()

Try the free JavaScript compiler

Reorders the receiver and returns it. Default order compares defined values as strings. A consistent comparator returns negative, zero or positive; ties are stable. Ordinary holes sort after undefined. No comparator call count is guaranteed.

Formal Syntax & Structure

javascript syntaxjavascript
array.sort(compareFn?)

Parameters & Attributes

NameTypeDescription
compareFnfunctionOptional ordering comparator; defaults to string ordering.

Return Value

Array

The same sorted receiver.

Working Example

Example Snippetjavascript
const a=[8,10,2]; const b=a.sort((x,y)=>x-y); console.log(b.join(","),a===b);
JavaScript Reference