JavaScript TypedArray toSorted() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2023

Definition

typedArray.toSorted() is the copying version of sort(). It returns a new typed array of the same type with the elements in numeric order, and the original stays as it is. Like sort(), it can take a comparator function. It was added in ES2023, together with toReversed() and with(). The new array has its own ArrayBuffer.

Remember
typedArray.toSorted()            → NEW typed array, numeric order
typedArray.toSorted(compareFn)   → your order

const a = new Uint8Array([10, 9, 1]);
a.toSorted()                     // 1 9 10
a                                // still 10 9 1

sort()     → changes the original
toSorted() → leaves it alone

Syntax

The comparator is optional. Without it, the copy is sorted from smallest to largest.

JavaScript
typedArray.toSorted()
typedArray.toSorted(compareFn)
CallWhat it does
ta.toSorted()Get a copy in numeric order, smallest first.
ta.toSorted((a, b) => b - a)Get a copy from the largest value to the smallest.
ta.toSorted().at(-1)Read the biggest value without sorting the original.

Added in ES2023 for all 11 typed array types. Older browsers do not have it; use ta.slice().sort() there.

Library declarations
JavaScript
TypedArray.prototype.toSorted(compareFn)   // returns a new typed array

// same type and length as the original, new ArrayBuffer
// the original typed array is not modified

Parameters

ParameterDescription
compareFn (optional)A function (a, b) that returns a negative number, a positive number, or 0, just like for sort(). Anything else (not a function and not undefined) throws a TypeError.

Return Value

Return ValueDescription
TypedArrayA new sorted typed array of the same type and length. It does not share memory with the original.

JavaScript Examples

Example 1 — Sort Without Touching the Original

Delivery times in minutes are stored in a Uint8Array. We want them in order, but the original arrival order must stay.

JavaScript
const arrival = new Uint8Array([10, 9, 1]);
const ordered = arrival.toSorted();

console.log(arrival.join(' '));      // 10 9 1
console.log(ordered.join(' '));      // 1 9 10
console.log(ordered === arrival);    // false
Try It Yourself

How It Works

  1. Numeric order in the copy. The new array reads 1 9 10. The number 9 is smaller than 10, because typed arrays do not compare text.
  2. Arrival order is kept. arrival still prints 10 9 1, so the first measurement is still the first element.
  3. A separate object comes back. The strict comparison prints false, because toSorted() does not return the array it was called on.

Example 2 — Largest First With a Comparator

An Int16Array of temperatures is sorted from hottest to coldest. The comparator is the same one that sort() uses.

JavaScript
const temps = new Int16Array([3, -1, 22, 15]);
const hottest = temps.toSorted((a, b) => b - a);

console.log(hottest.join(' '));                 // 22 15 3 -1
console.log(temps.join(' '));                   // 3 -1 22 15
console.log(hottest.buffer === temps.buffer);   // false
Try It Yourself

How It Works

  1. The comparator picks the order. When b - a is positive, b is hotter, so it moves before a. The hottest value is first.
  2. The source is unchanged. The original still has 3, -1, 22, 15 in their first order.
  3. No shared bytes. The two buffer objects are different, so the sorted copy is real, independent memory.

Example 3 — Find a Median From a Sorted Copy

To find the middle reading we need sorted data. A copy lets us look at the order while the readings stay in time order.

JavaScript
const readings = new Int16Array([5, -1, 3]);

const sorted = readings.toSorted();
const middle = sorted[Math.floor(sorted.length / 2)];
console.log(middle);                    // 3

sorted[0] = 100;
console.log(readings.join(' '));        // 5 -1 3
console.log(sorted.constructor.name);   // Int16Array
Try It Yourself

How It Works

  1. The sorted copy is -1 3 5. In that order, the element at index Math.floor(3 / 2), which is 1, is the middle value 3.
  2. Edit the copy freely. Setting sorted[0] = 100 changes only the copy, so readings still prints 5 -1 3.
  3. Still an Int16Array. The copy keeps the element type, so you can store, wrap, and compare numbers exactly as before.

Browser & Runtime Support

TypedArray.prototype.toSorted() was added in ES2023, together with toReversed() and with().

✓ Baseline · ES2023

TypedArray.prototype.toSorted()

Supported in recent versions of Chrome, Edge, Firefox, Safari, and Node.js.

93% Recent browsers only
Google Chrome Chrome 110 and newer
Full support
Mozilla Firefox Firefox 115 and newer
Full support
Apple Safari Safari 16 and newer
Full support
Microsoft Edge Edge 110 and newer
Full support
Node.js Node.js 20 and newer
Supported
Deno Recent versions
Supported
Internet Explorer No support
Not supported
toSorted() Recent browsers

Bottom line: Use it in recent browsers and Node.js 20 or newer. For older ones, write ta.slice().sort(), which does the same job.

Frequently Asked Questions

sort() reorders the original and returns it. toSorted() builds a new typed array in sorted order and leaves the original alone.
Yes. Like sort(), it compares numbers, so 9 comes before 10. A plain Array without a comparator would compare text.
It is an ES2023 method: Chrome 110, Edge 110, Firefox 115, Safari 16, and Node.js 20. For older browsers use slice().sort().
Yes. Pass (a, b) => b - a for largest first. If you pass something that is not a function, a TypeError is thrown.
Yes. A Float32Array gives a Float32Array and an Int16Array gives an Int16Array, each with its own new ArrayBuffer.

Next: toString()

Turn a typed array into text, with the numbers separated by commas.

toString() tutorial →

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