d3.js sorting by rollup field

前端 未结 3 836
面向向阳花
面向向阳花 2021-01-21 08:24

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\":         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-21 08:39

    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;})
                        .entries(data)
                        .sort(function(a, b){ return d3.ascending(a.values, b.values); })
    
    console.log(JSON.stringify(countryCount));
    

提交回复
热议问题