Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/writetome51/order-numerically
Re-orders array in ascending numeric order
https://github.com/writetome51/order-numerically
ascending javascript numbers order sort typescript
Last synced: 12 days ago
JSON representation
Re-orders array in ascending numeric order
- Host: GitHub
- URL: https://github.com/writetome51/order-numerically
- Owner: writetome51
- License: mit
- Created: 2018-10-06T05:14:44.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-23T02:56:17.000Z (about 4 years ago)
- Last Synced: 2024-11-03T22:29:53.571Z (17 days ago)
- Topics: ascending, javascript, numbers, order, sort, typescript
- Language: JavaScript
- Homepage:
- Size: 50.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# orderNumerically(
arr: any[]
getValueToSortBy? = (element) => element
): voidRe-orders `arr` in ascending numeric order. Uses super-fast [node-timsort](https://github.com/mziccard/node-timsort) for the actual sorting.
It sorts using this comparison function: `(a, b) => getValueToSortBy(a) - getValueToSortBy(b)`.
Optional callback `getValueToSortBy(element)` must return a number. By default it simply
returns the passed `element`.## Examples
```js
// A basic number sort:
let numbers = [10, 6, 44, 2, 21, 66, 32, 44];
orderNumerically(numbers);
console.log(numbers);
// [ 2, 6, 10, 21, 32, 44, 44, 66 ]// sort objects by property 'age'
let objects = [{age: 12}, {age: 7}, {age: 18}, {age: 5}];
orderNumerically(objects, (obj) => obj.age);
console.log(objects);
// [ {age: 5}, {age: 7}, {age: 12}, {age: 18} ]// sort arrays by length:
let arrays = [ [1,2,3], [1], [], [4,5], [0], [6,7], [4,5,6] ];
orderNumerically(arrays, (arr) => arr.length);
console.log(arrays);
// [ [], [1], [0], [4,5], [6,7], [1,2,3], [4,5,6] ]
```## Installation
`npm i @writetome51/order-numerically`## Loading
```js
import {orderNumerically} from '@writetome51/order-numerically';
```