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.
TypedArray 实例的 sort() 方法对类型化数组 到位 的元素进行排序,并返回对同一类型化数组(现已排序)的引用。此方法与 Array.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.
Try it
语法
参数
返回值
描述
¥Description
详细信息请参见 Array.prototype.sort()。此方法不是通用的,只能在类型化数组实例上调用。
¥See Array.prototype.sort() for more details. This method is not generic and can only be called on typed array instances.
示例
使用排序()
¥Using sort()
有关更多示例,另请参阅 Array.prototype.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 ]
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-%typedarray%.prototype.sort |
浏览器兼容性
BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.