typedArray.sort() puts the elements in order in place and returns the same typed array. The big difference from Array sort() is that the default order is numeric: 10 comes after 9. A plain Array turns the items into text first, so [10, 9, 1] becomes [1, 10, 9]. You can pass a comparator function to choose another order, for example from largest to smallest. If the original must stay, use toSorted().
Remember
typedArray.sort() → numeric, ascending, in place
typedArray.sort(compareFn) → your order
new Int16Array([10, 9, 1]).sort() → 1 9 10
[10, 9, 1].sort() (plain Array) → 1 10 9
Float arrays: -Infinity ... -0, 0 ... Infinity, NaN last
ta.sort() === ta → true
Syntax
The comparator is optional. Without it, the elements are sorted from smallest to largest.
JavaScript
typedArray.sort()
typedArray.sort(compareFn)
Call
What it does
ta.sort()
Sort numerically, smallest first. Floats put -0 before 0 and NaN last.
ta.sort((a, b) => b - a)
Sort from the largest value to the smallest.
ta.subarray(1, 4).sort()
Sort only a window of the array. The view shares memory, so the parent changes too.
Built into JavaScript since ES2015 on all 11 typed array types. The comparator must be a function or undefined; anything else throws a TypeError.
Library declarations
JavaScript
TypedArray.prototype.sort(compareFn) // returns the same typed array
// compareFn(a, b) -> negative: a first, positive: b first, 0: keep order
// default (no compareFn): numeric ascending order
Parameters
Parameter
Description
compareFn (optional)
A function that takes two elements a and b and returns a number. Negative means a goes first, positive means b goes first, and 0 means they are equal. A value that is not a function or undefined throws a TypeError.
Return Value
Return Value
Description
TypedArray
The same typed array, sorted. ta.sort() === ta is true. It is stable: equal elements keep their order.
JavaScript Examples
Example 1 — Sorting Is Numeric by Default
The same three numbers are sorted twice: once in an Int16Array and once in a plain Array. The orders are different.
Infinity ends are in the right place.-Infinity is smaller than every number, so it is first. A normal number such as 3 is placed by its value.
Minus zero comes before plus zero. The two zeros are equal for ===, but the sort puts -0 in front of 0. Object.is shows it.
NaN is always last. NaN is not smaller or bigger than anything, so the default sort moves it to the end of the array.
Compatibility
Browser & Runtime Support
TypedArray.prototype.sort() is part of ES2015 (ES6), together with typed arrays and the other array-like methods.
✓ Baseline · ES2015
TypedArray.prototype.sort()
Supported in every modern browser, plus Node.js, Deno, and Bun.
98%Modern browser support
Google ChromeAll modern versions
Full support
Mozilla FirefoxAll modern versions
Full support
Apple SafariAll modern versions
Full support
Microsoft EdgeAll modern versions
Full support
Node.jsAll current releases
Supported
DenoAll releases
Supported
Internet ExplorerIE 10 and 11 have typed arrays, but not this method
Not supported
sort()Excellent
Bottom line: Safe everywhere modern JavaScript runs. Internet Explorer 10 and 11 have typed arrays but not this method, so copy to a plain Array there before sorting.
Frequently Asked Questions
Numeric. Without a comparator it sorts from the smallest to the largest number, so 9 comes before 10. Only plain Arrays sort as text by default.
Yes. It sorts in place and returns the same typed array. Use toSorted() (ES2023) or slice().sort() to keep the original untouched.
NaN goes last, and -0 goes right before 0. For example, [NaN, 0, -0, 1] becomes -0, 0, 1, NaN.
A TypeError is thrown, for example for sort(null) or sort("x"). Pass a function, or nothing at all (undefined is fine).
Pass the comparator (a, b) => b - a. You can also sort normally and then call reverse(), which flips the sorted array in place.