JavaScript Array toSorted() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.toSorted(compareFn?) returns a new sorted array. The original stays unchanged — unlike sort(), which mutates in place. With no compare function, values become strings and sort by UTF-16 order (fine for words; wrong for most numbers). Pass the same compareFn you would use with sort().

Remember
array.toSorted()
array.toSorted(compareFn)

default  → string order (UTF-16)
numbers  → always pass (a, b) => a - b

original untouched ✓
sort() mutates in place

assign the result → const sorted = arr.toSorted()

Syntax

Call array.toSorted. Start with this line.

JavaScript
array.toSorted(compareFn?)
CallWhat it does
array.toSorted(compareFn?)Primary signature.
array.toSorted(...)Call with the arguments your case needs.

Built into JavaScript. Call toSorted(...) on an array instance — no import required.

Library declarations
JavaScript
array.toSorted(compareFn?)

Parameters

ParameterDescription
compareFnOptional. Same rules as sort(). Return negative if a before b, zero if equal, positive if a after b. Ascending numbers: (a, b) => a - b

Return Value

Return ValueDescription
*New sorted array (original unchanged)

JavaScript Examples

Example 1 — Default String Sort

Sort words alphabetically and keep the original array intact.

JavaScript
const fruits = ["banana", "apple", "cherry"];
const sorted = fruits.toSorted();

console.log(sorted);
console.log(fruits);
Try It Yourself

How It Works

  1. Start with fruits. Order is banana, apple, cherry.
  2. Call toSorted() with no compare. Values sort as strings (UTF-16).
  3. sorted is alphabetical. New array: apple, banana, cherry.
  4. Original stays. fruits is still banana, apple, cherry.

Example 2 — Numbers Ascending

Pass (a, b) => a - b for correct numeric order.

JavaScript
const nums = [10, 2, 1, 40];
const ascending = nums.toSorted((a, b) => a - b);

console.log(ascending);
console.log(nums);
Try It Yourself

How It Works

  1. Compare by value. a - b is negative when a is smaller.
  2. Smaller first. Negative results place a before b.
  3. Ascending copy. Result is [1, 2, 10, 40].
  4. Source intact. nums is still [10, 2, 1, 40].

Example 3 — Numbers Descending

Flip the compare function to (a, b) => b - a for high-to-low.

JavaScript
const scores = [40, 5, 100, 9];
const descending = scores.toSorted((a, b) => b - a);

console.log(descending);
console.log(scores);
Try It Yourself

How It Works

  1. Flip subtraction. b - a is negative when b is smaller — so larger values come first.
  2. High to low. Order becomes [100, 40, 9, 5].
  3. New array. descending is a copy; assign it to use the result.
  4. Original untouched. scores keeps [40, 5, 100, 9].

Browser & Runtime Support

Array.prototype.toSorted() was added in ES2023 with the other immutable array helpers (toReversed, with, toSpliced).

✓ Baseline · ES2023

Array.prototype.toSorted()

Supported in Chrome 110+, Firefox 115+, Safari 16+, Edge 110+, and Node.js 20+. Not available in Internet Explorer or very old browsers.

92% Modern browsers
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.toSorted() Good

Bottom line: For older targets, feature-detect with if ("toSorted" in Array.prototype) or fall back to [...arr].sort(compareFn).

Frequently Asked Questions

toSorted(compareFn?) returns a new array with the elements sorted. The original array is not modified. Without a compare function, elements are converted to strings and sorted by UTF-16 order.
No. toSorted() always returns a new sorted array. Assign the result to use it: const sorted = arr.toSorted(). Use sort() only when you want to mutate in place.
sort() reorders the original array in place. toSorted() leaves the original untouched and returns a sorted copy. Compare functions work the same way for both.
Default sort compares string versions: '1', '10', '2'. For numeric order, pass a compare function: (a, b) => a - b.
Return a negative number if a should come before b, zero if equal, positive if a should come after b. Ascending numbers: (a, b) => a - b. Descending: (a, b) => b - a.
toSorted() is ES2023. It works in Chrome 110+, Firefox 115+, Safari 16+, Edge 110+, and Node.js 20+. Use [...arr].sort(fn) as a fallback for older environments.

Next: toSpliced()

Learn the immutable splice helper—insert, remove, or replace without changing the original array.

toSpliced() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful