Skip to content

Instantly share code, notes, and snippets.

@tjstebbing
Created December 16, 2013 05:18
Show Gist options
  • Save tjstebbing/7982647 to your computer and use it in GitHub Desktop.
Save tjstebbing/7982647 to your computer and use it in GitHub Desktop.
function groupData(data, nGroups) {
var out = [];
data.sort(function(a, b) { return a > b; })
var hop = data[data.length-1] / nGroups;
for(var i=0; i<data.length; i++) {
var index = Math.ceil(data[i] / hop);
if(!out[index]) out[index] = [];
out[index].push(data[i]);
}
return out;
}
console.log(groupData([1,2,3,4,5,100, 40,600,6000], 10));
console.log(groupData([1,2,3,4,5], 5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment