Baseline 2023
Newly available
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.
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.
A new typed array with the elements sorted in ascending order.
DescriptionSee Array.prototype.toSorted()
for more details. This method is not generic and can only be called on typed array instances.
For more examples, see also the Array.prototype.sort()
method.
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 Browser compatibility See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4