项目作者: writetome51

项目描述 :
Re-orders array in ascending numeric order
高级语言: JavaScript
项目地址: git://github.com/writetome51/order-numerically.git
创建时间: 2018-10-06T05:14:44Z
项目社区:https://github.com/writetome51/order-numerically

开源协议:MIT License

下载


orderNumerically(
arr: any[]
getValueToSortBy? = (element) => element
): void

Re-orders arr in ascending numeric order. Uses super-fast 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

  1. // A basic number sort:
  2. let numbers = [10, 6, 44, 2, 21, 66, 32, 44];
  3. orderNumerically(numbers);
  4. console.log(numbers);
  5. // [ 2, 6, 10, 21, 32, 44, 44, 66 ]
  6. // sort objects by property 'age'
  7. let objects = [{age: 12}, {age: 7}, {age: 18}, {age: 5}];
  8. orderNumerically(objects, (obj) => obj.age);
  9. console.log(objects);
  10. // [ {age: 5}, {age: 7}, {age: 12}, {age: 18} ]
  11. // sort arrays by length:
  12. let arrays = [ [1,2,3], [1], [], [4,5], [0], [6,7], [4,5,6] ];
  13. orderNumerically(arrays, (arr) => arr.length);
  14. console.log(arrays);
  15. // [ [], [1], [0], [4,5], [6,7], [1,2,3], [4,5,6] ]

Installation

npm i @writetome51/order-numerically

Loading

  1. import {orderNumerically} from '@writetome51/order-numerically';