1 |
|
2 |
|
3 | # Function sort
|
4 |
|
5 | Sort the items in a matrix.
|
6 |
|
7 |
|
8 | ## Syntax
|
9 |
|
10 | ```js
|
11 | math.sort(x)
|
12 | math.sort(x, compare)
|
13 | ```
|
14 |
|
15 | ### Parameters
|
16 |
|
17 | Parameter | Type | Description
|
18 | --------- | ---- | -----------
|
19 | `x` | Matrix | Array | A one dimensional matrix or array to sort
|
20 | `compare` | Function | 'asc' | 'desc' | 'natural' | An optional _comparator function or name. The function is called as `compare(a, b)`, and must return 1 when a > b, -1 when a < b, and 0 when a == b. Default value: 'asc'.
|
21 |
|
22 | ### Returns
|
23 |
|
24 | Type | Description
|
25 | ---- | -----------
|
26 | Matrix | Array | Returns the sorted matrix.
|
27 |
|
28 |
|
29 | ## Examples
|
30 |
|
31 | ```js
|
32 | math.sort([5, 10, 1]) // returns [1, 5, 10]
|
33 | math.sort(['C', 'B', 'A', 'D'], math.compareNatural)
|
34 | // returns ['A', 'B', 'C', 'D']
|
35 |
|
36 | function sortByLength (a, b) {
|
37 | return a.length - b.length
|
38 | }
|
39 | math.sort(['Langdon', 'Tom', 'Sara'], sortByLength)
|
40 | // returns ['Tom', 'Sara', 'Langdon']
|
41 | ```
|
42 |
|
43 |
|
44 | ## See also
|
45 |
|
46 | [filter](filter.md),
|
47 | [forEach](forEach.md),
|
48 | [map](map.md),
|
49 | [compare](compare.md),
|
50 | [compareNatural](compareNatural.md)
|
51 |
|
\ | No newline at end of file |