d3 provides a method sortKeys in nest function which will sort your nested list based on the key you selected. You can pass d3.ascending or d3.descending based on your requirement.

From your example:

var countryCount = d3.nest()
                    .key(function(d) { return d.Country; })

which will give you:



No, there is no built-in function giving the result you are after. d3.nest() does have a methode nest.sortValues() which will sort the leaf elements of nested data, but this is meaningless in your case since you did apply .rollup() leaving you with just one leaf per key. As you already mentioned, the way to go is using Array.prototype.sort().


D3 provide the condition, ascending descending and you can use inside on sort method. No worries You are using a native javascript method with nice stability

var countryCount = d3.nest()
                    .key(function(d) { return d.Country; })
                    .rollup(function(a){return a.length;})
                    .sort(function(a, b){ return d3.ascending(a.values, b.values); })


Related Query

More Query from same tag