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

Array.toSorted

Prev: at | Up: Array | Next: from
Rel: Array, Array & Typed Arrays Guide
Ref: MDN Web Docs — Array.toSorted
Tags: #public #web #javascript #builtins #array #tier/core


Signature & Overview

arr.toSorted(compareFn?)  /* + toReversed(), toSpliced(), with() */

Non-mutating ES2023 counterpart to sort() returning a newly sorted array.

Notes & Pitfalls

Example

const orig = [3, 1, 2];
const asc = orig.toSorted((a, b) => a - b);
console.assert(JSON.stringify(orig) === "[3,1,2]");
console.assert(JSON.stringify(asc) === "[1,2,3]");

Linked from

Related notes