I am having a JSON data and i want to group by a field and then sort by the count.
var data = [{"Name":"Ravi","Country":"India"},
{"Name":"Alex","Country":"USA"},
{"Name":"Stew","Country":"UK"},
{"Name":"Mary","Country":"India"},
{"Name":"Raju","Country":"India"},
{"Name":"Bill","Country":"UK"},
{"Name":"Elisa","Country":"UK"},
{"Name":"Sharma","Country":"India"}];
and my d3.js query is the following
var countryCount = d3.nest()
.key(function(d) { return d.Country; })
.rollup(function(a){return a.length;})
.entries(data);
console.log(JSON.stringify(countryCount));
and my output is
[{"key":"India","values":4},{"key":"USA","values":1},{"key":"UK","values":3}]
by my desired output is ( sorted by rollup value)
[{"key":"India","values":4},{"key":"UK","values":3},{"key":"USA","values":1}]
How can i do this? I know that the following browser default sort method also gives desired output. But just want to clear whether d3.js provides any inbuilt method to achieve this.
console.log(JSON.stringify(countryCount.sort(function (a, b){
if (a.values > b.values) {return -1;}
else if (a.values < b.values) { return 1;}
else return 0;
})));