Function sort #

对矩阵中的项进行排序。

Syntax #

math.sort(x)
math.sort(x, compare)

Parameters #

Parameter Type Description
x 矩阵 | 数组 要排序的一维矩阵或数组
compare Function | ‘asc’ | ‘desc’ | ‘natural’ 一个可选的 _比较器函数或名称。该函数被调用为 compare(a, b),并且在 a > b 时必须返回 1,在 a < b 时返回 -1,在 a == b 时返回 0。默认值为 ‘asc’。

Returns #

Type Description
矩阵 | 数组 返回已排序的矩阵。

Throws #

Type | Description —- | ———–

Examples #

math.sort([5, 10, 1]) // returns [1, 5, 10]
math.sort(['C', 'B', 'A', 'D'], math.compareNatural)
// returns ['A', 'B', 'C', 'D']

function sortByLength (a, b) {
  return a.length - b.length
}
math.sort(['Langdon', 'Tom', 'Sara'], sortByLength)
// returns ['Tom', 'Sara', 'Langdon']

另请参阅 #

filter, forEach, map, compare, compareNatural

Fork me on GitHub