Content-Length: 178480 | pFad | http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toSorted

TypedArray.prototype.toSorted() - JavaScript | MDN

TypedArray.prototype.toSorted()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2023.

The toSorted() method of TypedArray instances is the copying version of the sort() method. It returns a new typed array with the elements sorted in ascending order. This method has the same algorithm as Array.prototype.toSorted(), except that it sorts the values numerically instead of as strings by default.

Syntax

js
toSorted()
toSorted(compareFn)

Parameters

compareFn Optional

A function that determines the order of the elements. If omitted, the typed array elements are sorted according to numeric value. See sort() for more information.

Return value

A new typed array with the elements sorted in ascending order.

Description

See Array.prototype.toSorted() for more details. This method is not generic and can only be called on typed array instances.

Examples

Sorting an array

For more examples, see also the Array.prototype.sort() method.

js
const numbers = new Uint8Array([40, 1, 5, 200]);
const numberSorted = numbers.toSorted();
console.log(numberSorted); // Uint8Array [ 1, 5, 40, 200 ]
// Unlike plain Arrays, a compare function is not required
// to sort the numbers numerically.
console.log(numbers); // Uint8Array [ 40, 1, 5, 200 ]

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-%typedarray%.prototype.tosorted

Browser compatibility

See also









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toSorted

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy