mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
61 lines
1.7 KiB
Markdown
61 lines
1.7 KiB
Markdown
---
|
|
layout: default
|
|
---
|
|
|
|
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
|
|
|
|
<h1 id="function-sort">Function sort <a href="#function-sort" title="Permalink">#</a></h1>
|
|
|
|
Sort the items in a matrix.
|
|
|
|
|
|
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
|
|
|
|
```js
|
|
math.sort(x)
|
|
math.sort(x, compare)
|
|
```
|
|
|
|
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
|
|
|
|
Parameter | Type | Description
|
|
--------- | ---- | -----------
|
|
`x` | Matrix | Array | A one dimensional matrix or array to sort
|
|
`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'.
|
|
|
|
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
Matrix | Array | Returns the sorted matrix.
|
|
|
|
|
|
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
|
|
|
|
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
|
|
|
|
```js
|
|
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']
|
|
```
|
|
|
|
|
|
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
|
|
|
|
[filter](filter.html),
|
|
[forEach](forEach.html),
|
|
[map](map.html),
|
|
[compare](compare.html),
|
|
[compareNatural](compareNatural.html)
|