# Simple Arrange //JavaScript Repository

## Description

Mathematical arrange on array elements.
Created: 2005.11.28

``````//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/simple-arrange [rev. #1]

simpleArrange = function(a, n, m){
var o = a;
if(n >= o.length) return [];
for(var j, l, k, p, f, r, q = k = 1, i = (l = o.length) + 1, j = l - n; --i; i <= j ? q *= i : k *= i);
for(x = [new Array(n), new Array(n), new Array(n), new Array(n)], j = q = k * q / q, k = l + 1, i = -1;
++i < n; x[2][i] = i, x[1][i] = x[0][i] = j /= --k);
for(r = new Array(q), p = -1; ++p < q;)
for(r[p] = new Array(n), i = -1; ++i < n; !--x[1][i] && (x[1][i] = x[0][i],
x[2][i] = (x[2][i] + 1) % l), r[p][i] = m ? x[3][i] : o[x[3][i]])
for(x[3][i] = x[2][i], f = 0; !f; f = !f)
for(j = i; j;)
if(x[3][--j] == x[2][i]){
x[3][i] = x[2][i] = (x[2][i] + ++f) % l;
break;
}
return r;
};``````

## Example (Example)

``````<script type="text/javascript">
//<![CDATA[

var a = ["A", "B", "C", "D"], q = 3, j = simpleArrange(a, q);
document.write(
"<h2>", a.join(" - "), " : ", q, " = ", j.length, "</h2>",
j.join("<br />")
);

//]]>
</script>``````

## Help

simpleArrange(vector: Array, n: Integer, [useIndex: Boolean = false]): Array
Do the mathematical simple arrange, groups made up by n elements (n < array size), where the elements are distincted by the order or value. The function returns a bidimensional array, where line = arrange and column = element.
vector
array that will be arranged
n
amount of elements in each group
useIndex
defines the return type, if true, returns just a "map" containing the indexes of the vector argument, otherwise, it uses the values of the array

2.04