array-multisort

1.0.1 • Public • Published

js-arrayMultisort

Zero-dependency array multi-sort with O(N*log(N)) complexity. The sort is not necessarily stable. Returns a new copy for each sorted array in the list.

arrayMultisort((a, b) => (- b))([3, 4, 1, 2], ['A', 'B', 'C', 'D'])
// returns [[1, 2, 3, 4], ['C', 'D', 'A', 'B']]

Syntax

arrayMultisort(compareFunction)(referenceArray[, anotherArray1, anotherArray2, ...])

Parameters

compareFunction

Specifies a function that defines the sort order. Same as for Array.sort.

referenceArray

The array of elements are sorted according to the return value of the compare function.

anotherArray1-N

Order of elements in these another arrays is changed in the same way as in referenceArray. If element on index 1 of the referenceArray was moved to index 5 of the sorted array, element on index 1 of anotherArray1 is moved to a fifth index.

Return value

List of sorted arrays [[referenceArray], [anotherArray1], ...]. All arrays are sorted in the same order as the referenceArray.

Usage

Example 1:

arrayMultisort((a, b) => (- b))([3, 4, 1, 2], ['A', 'B', 'C', 'D'])

Output: [[1, 2, 3, 4], ['C', 'D', 'A', 'B']]

Example 2:

const compare = (a, b) => a.localeCompare(b)
const sort = arrayMultisort(compare)
sort(['c', 'b', 'a'], [1, 2, 3])

Output: [['a', 'b', 'c'], [3, 2, 1]]

Package Sidebar

Install

npm i array-multisort

Weekly Downloads

3

Version

1.0.1

License

MIT

Last publish

Collaborators

  • vaclav-purchart