JavaScript TypedArray sort() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

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)
CallWhat 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

ParameterDescription
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 ValueDescription
TypedArrayThe 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.

JavaScript
const typed = new Int16Array([10, 9, 1]);
const plain = [10, 9, 1];

typed.sort();
plain.sort();

console.log(typed.join(' '));   // 1 9 10
console.log(plain.join(' '));   // 1 10 9

const mixed = new Int16Array([10, 9, 100, -1]).sort();
console.log(mixed.join(' '));   // -1 9 10 100
Try It Yourself

How It Works

  1. Typed arrays compare numbers. The Int16Array is sorted by value, so 9 comes before 10, and the result reads 1 9 10.
  2. Plain Arrays compare text. The Array turns each item into a string, and the text "10" sorts before "9", so it prints 1 10 9.
  3. Negatives are handled too. In the third line, -1 is the smallest number, so it comes first. No comparator was needed.

Example 2 — A Comparator for Largest First

Game scores are stored in a Uint8Array. We sort them from best to worst and show that the same array was changed.

JavaScript
const scores = new Uint8Array([55, 200, 30, 120, 90]);

const sorted = scores.sort((a, b) => b - a);
console.log(scores.join(' '));                  // 200 120 90 55 30
console.log(sorted === scores);                 // true
console.log(scores.subarray(0, 3).join(' '));   // 200 120 90
Try It Yourself

How It Works

  1. The comparator reverses the order. For two scores, b - a is positive when b is bigger, so the bigger score is moved in front.
  2. The original array is the result. scores itself now holds the sorted values, and sorted === scores prints true.
  3. The top three are easy to read. subarray(0, 3) is a window on the first three elements, which are now the three best scores.

Example 3 — Where -0 and NaN End Up

A Float64Array can hold NaN, infinities, and both zeros. The default sort has a fixed place for each of them.

JavaScript
const data = new Float64Array([NaN, 0, -0, 3, -Infinity, 1]);
data.sort();

const labels = Array.from(data, (x) => (Object.is(x, -0) ? '-0' : String(x)));
console.log(labels.join(' '));        // -Infinity -0 0 1 3 NaN
console.log(Object.is(data[1], -0));  // true
console.log(Object.is(data[5], NaN)); // true
Try It Yourself

How It Works

  1. 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.
  2. 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.
  3. NaN is always last. NaN is not smaller or bigger than anything, so the default sort moves it to the end of the array.

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 Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer IE 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.

Next: subarray()

Create a view on part of the same buffer, without copying any data.

subarray() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful