array-order

sort an array in-place to a preset order

Usage no npm install needed!

<script type="module">
  import arrayOrder from 'https://cdn.skypack.dev/array-order';
</script>

README

array-order

collection of in-place sorting of arrays

SummaryExampleExportsLicense

summary

  • rank(a:ArrayLike, [,compare]) => ranks:ArrayLike
  • order(a:ArrayLike, [,compare]) => orders:ArrayLike
  • flip(a:ArrayLike) => a ranks==>orders; orders==>ranks
  • pick(order:ArrayLike, a:ArrayLike) => a performs reordering based on given ranks
  • sort(ref:ArrayLike, [,dir]) => (src:ArrayLike, [,tgt]) => ArrayLike

...and more (permute, derange, sort, ...), see individual files for descriptions and examples most function have a in-place and to-target version depending on the arguments

example

import sort from 'array-order/sort.js'

const columns = [[2,1,0,3], ['c','b','a','d']]
columns.forEach(sort(columns[0]))
//each column now sorted according to the first one
//columns == [ [0,1,2,3], ['a','b','c','d'] ]

exports

  • named: import {sort} from 'array-order'
  • default: import rank from 'array-order/rank.js'

sort order

the sort factory can take an additional compare function to change the sort order: (ref:Array [,compare]) => (a:Array) => a

  • undefined increasing numeric order
  • any negative number decreasing numeric order
  • a normal compare function just like Array.prototype.sort: (a, b) => a - b

License

MIT © Hugo Villeneuve