JSPM

@writetome51/order-numerically

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q12735F
  • License MIT

Function takes an array of numbers and returns new array with numbers in numeric order

Package Exports

  • @writetome51/order-numerically

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@writetome51/order-numerically) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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

// 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

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