Array.sort
Array.sort
Function
Sort the array. When no callback is passed, the order is sorted by the element string value; when the callback is passed, the order is determined by the number returned by the callback.
Syntax
array.sort(callback)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| callback | Fn | No | None | The comparison function signature is fn(left, right), return less than or equal to 0 to maintain the order, return greater than 0 to exchange. |
Return value
| Type | Description |
|---|---|
| Array | Returns the original array object. |
Example
items = [3, 1, 2] result = items.sort() // Output: [1,2,3] print result
Notes
- This method will modify the original array.
- Callback sorting uses stable merge sorting.