TypedArray.prototype.sort()
Baseline Widely availableThis feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
>The sort() method of TypedArray instances sorts the elements of a typed array in place and returns the reference to the same typed array, now sorted. This method has the same algorithm as Array.prototype.sort(), except that it sorts the values numerically instead of as strings by default.
Try it
const uint8 = new Uint8Array([40, 10, 50, 20, 30]);
uint8.sort();
console.log(uint8);
// Expected output: Uint8Array [10, 20, 30, 40, 50]
Syntax
jssort()
sort(compareFn)
Parameters
compareFnOptional-
A function that determines the order of the elements. The function is called with the following arguments:
It should return a number where:
- A negative value indicates that
ashould come beforeb. - A positive value indicates that
ashould come afterb. - Zero or
NaNindicates thataandbare considered equal.
To memorize this, remember that
(a, b) => a - bsorts numbers in ascending order.If omitted, the typed array elements are sorted according to numeric value.
- A negative value indicates that
Return value
The reference to the original typed array, now sorted. Note that the typed array is sorted in place, and no copy is made.
Description
See Array.prototype.sort() for more details. This method is not generic and can only be called on typed array instances.
Examples
>Using sort()
For more examples, see also the Array.prototype.sort() method.
let numbers = new Uint8Array([40, 1, 5, 200]);
numbers.sort();
// Uint8Array [ 1, 5, 40, 200 ]
// Unlike plain Arrays, a compare function is not required
// to sort the numbers numerically.
// Regular Arrays require a compare function to sort numerically:
numbers = [40, 1, 5, 200];
numbers.sort();
// [1, 200, 40, 5]
numbers.sort((a, b) => a - b); // compare numbers
// [ 1, 5, 40, 200 ]
Specifications
| Specification |
|---|
| ECMAScript® 2027 Language Specification > # sec-%typedarray%.prototype.sort > |
Browser compatibility
See also
- Polyfill of
TypedArray.prototype.sortwith modern behavior like stable sort incore-js - JavaScript typed arrays guide
TypedArrayTypedArray.prototype.reverse()TypedArray.prototype.toSorted()Array.prototype.sort()
Help improve MDN
Was this page helpful to you? Yes No Learn how to contributeThis page was last modified on Oct 26, 2026 by MDN contributors.
View this page on GitHub • Report a problem with this content