The Javascript Int8Array sort()
method sorts the elements of a typed array numerically.
It returns the sorted typed array.
This method works the same as Array.prototype.sort()
, except that sorts the values numerically instead of as strings.
Int8Array.sort([compareFunction])
Parameter | Optional | Meaning |
---|---|---|
compareFunction | Optional | Specifies a function that defines the sort order. |
Unlike plain Arrays, a compare function is not required to sort the numbers numerically.
Regular Arrays require a compare function to sort numerically:
let numbers = new Int8Array([40, 1, 5, 200]); numbers.sort();/*from w w w. ja va2s . com*/ numbers = [40, 1, 5, 200]; numbers.sort(); console.log(numbers); numbers.sort((a, b) => a - b); // compare numbers console.log(numbers);