Installation
Quick Take
Examples
API — sortByCol()
The main function sortByCol()
is imported like this:
It is a function which takes two input arguments:
Input argument | Type | Obligatory | Description |
---|---|---|---|
arr Type: Array of zero or more arrays Obligatory: yes | |||
arr | Array of zero or more arrays | yes | Source of data to put into an AST |
index Type: Natural number or zero, like a number or string Obligatory: no | |||
index | Natural number or zero, like a number or string | no | By which column should we match the subarrays (rows)? The default is 0 or the first element of each sub-array. |
The function returns a sorted array.
API — version
You can import version
:
Purpose
Sorts array of arrays by any column (the default is the first element, zero’th column index).
The algorithm is tailored for integer-only values.
Consider this arrangement:
1 ----- 9 ----- 0
1 ---------------
1 ----- 8 ----- 2
1 ----- 7 ----- 5
In JS code, that’s:
[[1, 9, 0], [1], [1, 8, 2], [1, 7, 5]];
Default sorting is against first column (zero’th index), so result would be:
1 ----- 7 ----- 5
1 ----- 8 ----- 2
1 ----- 9 ----- 0
1 ---------------
Output in JS code:
[[1, 7, 5], [1, 8, 2], [1, 9, 0], [1]];
Rules:
- When we compare two rows, first we compare by particular column (default is first, zero-index column). Then, if values are equal, we look around and compare by those values. First, compare left-side, then right-side. Then, if values are equal even there, we “ripple” outwards. First, compare left-side, then right-side. Then, if values are equal even there, we “ripple” outwards. …
- We accept arrays, normalised into a matrix, with absent value fillings set to
null
. Same behaviour.
1 ---- 7 ------ 5
1 ---- 8 ------ 2
1 ---- 9 ------ 0
1 --- null -- null