sort.md (1210B)
1 <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> 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 ### Throws 30 31 Type | Description 32 ---- | ----------- 33 34 35 ## Examples 36 37 ```js 38 math.sort([5, 10, 1]) // returns [1, 5, 10] 39 math.sort(['C', 'B', 'A', 'D'], math.compareNatural) 40 // returns ['A', 'B', 'C', 'D'] 41 42 function sortByLength (a, b) { 43 return a.length - b.length 44 } 45 math.sort(['Langdon', 'Tom', 'Sara'], sortByLength) 46 // returns ['Tom', 'Sara', 'Langdon'] 47 ``` 48 49 50 ## See also 51 52 [filter](filter.md), 53 [forEach](forEach.md), 54 [map](map.md), 55 [compare](compare.md), 56 [compareNatural](compareNatural.md)