UNPKG

1.16 kBMarkdownView Raw
1<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
3# Function sort
4
5Sort the items in a matrix.
6
7
8## Syntax
9
10```js
11math.sort(x)
12math.sort(x, compare)
13```
14
15### Parameters
16
17Parameter | Type | Description
18--------- | ---- | -----------
19`x` | Matrix &#124; Array | A one dimensional matrix or array to sort
20`compare` | Function &#124; 'asc' &#124; 'desc' &#124; '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
24Type | Description
25---- | -----------
26Matrix &#124; Array | Returns the sorted matrix.
27
28
29## Examples
30
31```js
32math.sort([5, 10, 1]) // returns [1, 5, 10]
33math.sort(['C', 'B', 'A', 'D'], math.compareNatural)
34// returns ['A', 'B', 'C', 'D']
35
36function sortByLength (a, b) {
37 return a.length - b.length
38}
39math.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