TypedArray.prototype.sort()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

O método sort() ordena os elementos de uma matriz tipada no local e retorna a matriz ordenada. Esse método tem o mesmo algoritmo que Array.prototype.sort(). TypedArray é uma das maneiras de escrever matrizes (en-US).

Syntax

typedarray.sort([compareFunction])

Parâmetros

compareFunction Optional

Especifica uma função que define a ordem de classificação.

Valor de retorno

A matriz ordenada.

Exemplos

Para mais exemplos, acesse o método Array.prototype.sort().

js
var numbers = new Uint8Array([40, 1, 5, 200]);
numbers.sort();
// Uint8Array [ 1, 5, 40, 200 ]
// A compare function is not required as in the case of Array
// to sort the numbers numerically.

var numbers = [40, 1, 5, 200];
numbers.sort();
// The elements are sorted as strings.
// [1, 200, 40, 5]

function compareNumbers(a, b) {
  return a - b;
}

numbers.sort(compareNumbers);
// [ 1, 5, 40, 200 ]

Especificações

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.sort

Compatibilidade com navegadores

BCD tables only load in the browser

Ver também