I'm trying to sort and then slice a set of javascript objects. I'm successfully slicing the data, but the sort is returning a list of categories that is neither alphabetically sorted by category nor by value.
My CSV looks like this:
category,date_calc,value,YYYY,MM
string1,2011-08-01,46440.16,2011,8
string2,2013-03-01,68249.72,2013,3
string1,2014-01-01,4285,2014,1
string3,2012-03-01,47646.82,2012,3
...
This is my code:
d3.csv("data/ncc_category.csv", function(error, csv) {
var data = d3.nest()
.key(function(d) { return d.category;})
.rollup(function(d) { return d3.sum(d, function(g) {return g.value; }); }).entries(csv)
var sorted = data.sort(function (a, b) {
return a.value - b.value;
}).reverse();
var top10 = sorted.slice(0, 10); // slice
console.log(top10);
});
EDIT:
Screen grab for the first 4 objects in data
.
Screen grab for the first 4 objects in top10