Array.sort

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

Parameters

ParametersTypeRequiredDefault valueDescription
callbackFnNoNoneThe 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

TypeDescription
ArrayReturns the original array object.

Example

Notes

  • This method will modify the original array.
  • Callback sorting uses stable merge sorting.