On this page
  1. Array.sort
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

Array.sort

Prev: isArray | Up: Array | Next: splice
Rel: Array, Array & Typed Arrays Guide
Ref: MDN Web Docs — Array.sort
Tags: #public #web #javascript #builtins #array #tier/standard


Signature & Overview

arr.sort(compareFn?)

Sorts arr in place (mutates the original array) and returns arr.

Notes & Pitfalls

Example

const nums = [10, 2, 30, 1];
nums.sort((a, b) => a - b);
console.assert(JSON.stringify(nums) === "[1,2,10,30]");

Linked from

Related notes