am learning d3 at the moment (new to JavaScript too), and am having a bit of trouble getting my nested data to sort by 'rolled up' values. Here is my code fragment:
d3.csv("flat_data.csv", function (data) {
var nested_data = d3.nest()
.key(function(d) { return d.Field1; })
.rollup(function(g) {
return d3.sum(g, function(v) {return v.Field2; });
})
.entries(data)
.sort(function(a,b) {return d3.descending(a.values,b.values);});
console.log(nested_data);
Try as I might, nested_data is stubbornly unsorted (no specific error thrown). I would like the array entries(data) to be sorted by the values calculated by the rollup function (which are correct). Have looked at a few questions on SO and they at least one recommends the above syntax. I'm obviously missing something - could someone please help? Thank you!
EDIT:
So on consulting the API reference, maybe the sort method is old? looks like there is a nest.sortValues(comparator) method, if I can work out the correct syntax for my code I'll post here as didn't immediately work but maybe can find the right way to use it (feel free to let me know though!) Thanks again
EDIT2:
Totally foxed by this! tried to employ syntax in http://bl.ocks.org/phoebebright/raw/3176159/ amongst other things to no avail. It'd be fantastic if someone knew how to make it work! Thanks
EDIT3:
sortValues is unsuitable in this case since it sorts the leaves, of which there is only one in the case of a rolledup function. I think mystery solved as per 'answer' below. Thanks.