UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ TypedArray/ TypedArray.prototype.sort()

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.

Syntax

sort()
sort(compareFn)

Parameters

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

Browser compatibility

See also