group - nodef/extra-array GitHub Wiki

Keep similar values together and in order.

Similar: cut, split, group.

function group(x, fc, fm)
// x:  an array
// fc: compare function (a, b)
// fm: map function (v, i, x)
const xarray = require('extra-array');

var x = [1, 2, 2, -2, -2, 4];
xarray.group(x);
// → [ [ 1 ], [ 2, 2 ], [ -2, -2 ], [ 4 ] ]

xarray.group(x, (a, b) => Math.abs(a) - Math.abs(b));
// → [ [ 1 ], [ 2, 2, -2, -2 ], [ 4 ] ]

xarray.group(x, null, v => Math.abs(v));
// → [ [ 1 ], [ 2, 2, -2, -2 ], [ 4 ] ]

References