JSPM

  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q47152F
  • License MIT

sort array of arrays by column, rippling the sorting outwards from that column

Package Exports

  • array-of-arrays-sort-by-col

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 (array-of-arrays-sort-by-col) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

array-of-arrays-sort-by-col

sort array of arrays by column, rippling the sorting outwards from that column

Minimum Node version required Repository is on BitBucket Coverage View dependencies as 2D chart Downloads/Month Test in browser Code style: prettier MIT License

Install

npm i array-of-arrays-sort-by-col
// consume as CommonJS require():
const sortByCol = require("array-of-arrays-sort-by-col");
// or as ES Module:
import sortByCol from "array-of-arrays-sort-by-col";

Here's what you'll get:

Type Key in package.json Path Size
Main export - CommonJS version, transpiled to ES5, contains require and module.exports main dist/array-of-arrays-sort-by-col.cjs.js 4 KB
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. module dist/array-of-arrays-sort-by-col.esm.js 3 KB
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in browser dist/array-of-arrays-sort-by-col.umd.js 12 KB

⬆ back to top

Table of Contents

What it does

Sorts array of arrays by any column (default is first element, zero'th column index).

The algorithm is tailored for integer-only values.

Consider this input:

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

⬆ back to top

Sorting by certain column

For example, let's sort this array by second element (column index = 1):

const sortByCol = require("array-of-arrays-sort-by-col");
const input = [[1, 9, 0], [1], [1, 8, 2], [1, 7, 5]];
const result = sortByCol(input, 1);
console.log(
  `${`\u001b[${33}m${`input`}\u001b[${39}m`} = ${JSON.stringify(
    input,
    null,
    0
  )}`
);
// => input = [[1, 7, 5], [1, 8, 2], [1, 9, 0], [1]],

⬆ back to top

API

sortByCol (arr, [index])

API - Input

Input argument Type Obligatory? Description
arr Array of zero or more arrays yes Source of data to put into an AST
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.

⬆ back to top

API - Output

Type Description
Array of arrays Same thing as input but sorted (if given not empty)

⬆ back to top

Purpose of this library

It will be a cornerstone of generate-ifs. There we turn list of characters (for example, astral-ones, pieces of emoji) into JS code which checks, if particular index anywhere within any of given character sequences. All character variations (if "a" followed by "b" OR "b" preceded by "a") are gathered into a single matrix where "root" axis column is the index from which we start checking.

This library will sort according to that axis column.

Outside of this case, this library could be used to sort two-dimensional arrays of integers against certain column, with "rippling" comparison (as opposed to first match by certain column, but if they're equal, just iterate from zero-th to last, skipping "certain"-one).

Practically, in human terms, this library makes sure the values clump around the particular column and "float" to the top as much as possible.

⬆ back to top

Contributing

  • If you want a new feature in this package or you would like us to change some of its functionality, raise an issue on this repo.

  • If you tried to use this library but it misbehaves, or you need advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo.

  • If you would like to add or change some features, just fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Prettier is enabled, so you don't need to worry about the code style.

⬆ back to top

Licence

MIT License (MIT)

Copyright © 2018 Codsen Ltd, Roy Revelt