简体   繁体   English

d3.js按汇总字段排序

[英]d3.js sorting by rollup field

I am having a JSON data and i want to group by a field and then sort by the count. 我有一个JSON数据,我想按一个字段分组,然后按计数排序。

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 我的d3.js查询如下

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 . 但是只是想弄清楚d3.js是否提供了任何内置方法来实现此目的

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;
})));

D3 provide the condition, ascending descending and you can use inside on sort method . D3提供了条件, 上升 下降 ,你可以在里面使用的排序方法 No worries You are using a native javascript method with nice stability 不用担心,您正在使用具有良好稳定性的本机javascript方法

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));

d3 provides a method sortKeys in nest function which will sort your nested list based on the key you selected. d3在nest函数中提供了sortKeys方法,它将根据您选择的键对嵌套列表进行排序。 You can pass d3.ascending or d3.descending based on your requirement. 您可以根据需要传递d3.ascendingd3.descending

From your example: 从您的示例:

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

which will give you: 这将为您提供:

[{"key":"India","values":4},{"key":"UK","values":3},{"key":"USA","values":1}]

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. d3.nest()确实具有方法nest.sortValues() ,该方法nest.sortValues()嵌套数据的叶子元素进行排序,但这在您的情况下是没有意义的,因为您确实应用了.rollup() ,每个键只留下了一个叶子。 As you already mentioned, the way to go is using Array.prototype.sort() . 正如您已经提到的,方法是使用Array.prototype.sort()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM