对 Array 排序。
## 语法
~~~
arrayobj.sort(sortFunction)
~~~
## 参数
|参数|定义|
|arrayObj|必需。任意 Array 对象。
|sortFunction|可选。用来确定元素顺序的函数的名称。如果省略 ASCII 字符顺序,则将按升序对这些元素进行排序。|
## 返回值
已排序的数组。
## 备注
sort 方法就地对 Array 对象进行排序;在执行过程中不会创建新 Array 对象。
如果在 sortFunction 参数中提供一个函数,则该函数必须返回下列值之一:
- 如果所传递的第一个参数小于第二个参数,则返回负值。
- 如果两个参数相等,则返回零。
- 如果第一个参数大于第二个参数,则返回正值。
下面的示例演示如何使用 sort 方法。
~~~
var a = new Array(4, 11, 2, 10, 3, 1);
var b = a.sort();
document.write(b);
document.write("<br/>");
// This is ASCII character order.
// Output: 1,10,11,2,3,4)
// Sort the array elements with a function that compares array elements.
b = a.sort(CompareForSort);
document.write(b);
document.write("<br/>");
// Output: 1,2,3,4,10,11.
// Sorts array elements in ascending order numerically.
function CompareForSort(first, second)
{
if (first == second)
return 0;
if (first < second)
return -1;
else
return 1;
}
~~~
- ActiveXObject对象
- Array对象
- constructor属性
- length属性
- prototype属性
- Array.from函数
- Array.isArray函数
- Array.of函数
- concat方法
- entries方法
- every方法
- fill方法
- filter方法
- findIndex方法
- forEach方法
- indexOf方法
- join方法
- keys方法
- lastIndexOf方法
- map方法
- pop方法
- push方法
- reduce方法
- reduceRight方法
- reverse方法
- shift方法
- slice方法
- some方法
- sort方法
- splice方法
- toString方法
- unshift方法
- valueOf方法
- values方法
- ArrayBuffer对象
- byteLength属性
- ArrayBuffer.isView函数
- slice方法
- arguments对象
- 0...n 属性(参数)
- callee 属性(参数)
- length 属性 (arguments)
- Boolean对象
- constructor 属性(布尔值)
- prototype 属性(布尔值)
- toString 方法 (Boolean)
- valueOf 方法 (Boolean)
- DataView对象
- buffer属性 (DataView)
- byteLength属性(DataView)
- byteOffset属性(DataView)
- getInt8方法(DataView)
- getUint8方法(DataView)
- getInt16方法(DataView)
- getUint16方法(DataView)
- getInt32方法(DataView)
- getUint32方法(DataView)
- getFloat32方法(DataView)
- getFloat64方法(DataView)
- setInt8方法(DataView)
- setUint8方法(DataView)
- setInt16方法(DataView)
- setUint16方法(DataView)
- setInt32方法(DataView)
- setUint32方法(DataView)
- setFloat32方法(DataView)
- setFloat64方法(DataView)
- Date对象
- Debug对象
- Enumerator对象
- Error对象
- Float32Array对象
- Float64Array对象
- Function对象
- Global对象
- Int8Array对象
- Int16Array对象
- Int32Array对象
- Intl.Collator对象
- Intl.DateTimeFormat对象
- Intl.NumberFormat对象
- JSON对象
- Map对象
- Math对象
- Number对象
- Object对象
- Promise对象
- 代理对象
- Reflect对象
- RegExp对象
- 正则表达式对象
- Set对象
- String对象
- 符号对象
- Uint8Array对象
- Uint8ClampedArray对象
- Uint16Array对象
- Uint32Array对象
- VBArray对象
- WeakMap对象
- WeakSet对象
- WinRTError对象