TypedArray.prototype.toSorted() - JavaScript | MDN
Syntax
js
toSorted()
toSorted(compareFn)
Parameters
compareFnOptional-
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® 2027 Language Specification # sec-%typedarray%.prototype.tosorted |