简体   繁体   English

两个对象数组正在合并,但它们没有排序 - D3

[英]Two array of objects are merging, but they're not sorting - D3

I've read this [SO post][1], it has helped, but it looks like it has played around with the data...我读过这篇 [SO post][1],它有帮助,但看起来它已经处理了数据......

I have read in two CSV files that look like this:我已经阅读了两个如下所示的 CSV 文件:

word, frequency
random, 462546
stupid, 34652
dumb, 4346

I've merged them, which works.我已经合并了它们,这很有效。 I've sorted them, however, it half works.我已经对它们进行了排序,但是,它成功了一半。 The sorting function sorts the two array of objects as if they were separate.排序函数对两个对象数组进行排序,就好像它们是分开的一样。 What I mean by this, is that my two arrays of objects merge together, but it has merged them one after another.我的意思是,我的两个对象数组合并在一起,但它已将它们一个接一个合并。 Then sorted one array of objects, then sorted the other, without sorting them as one whole array, it's sorting them as two arrays.然后对一个对象数组进行排序,然后对另一个对象进行排序,没有将它们作为一个完整的数组进行排序,而是将它们作为两个数组进行排序。

A link to my CSV files is here enter link description here我的 CSV 文件的链接在这里输入链接描述在这里

d3.csv("data/ArsenalDictionary.csv", function(error1, Arsenal) {
    d3.csv("data/ChelseaDictionary.csv", function(error2, Chelsea) {

        var selected = d3.merge([Arsenal, Chelsea]);

        selected.sort(function(a, b){ return d3.descending(a[2], b[2]); })

        console.log(selected);

    });
});

Your array selected isn't getting sorted because you are attempting to sort the objects by a non-existent property.selected数组未排序,因为您试图按不存在的属性对对象进行排序。

The elements of your array are objects with two properties, "words" and " frequency" (note the leading space in the latter).数组的元素是具有两个属性的对象, "words"" frequency" (注意后者的前导空格)。 You are attempting to sort them by a property named 2 , which they don't have.您正在尝试通过名为2的属性对它们进行排序,而它们没有。

You would have better luck sorting them by the frequency property:您会更幸运地按频率属性对它们进行排序:

    selected.sort(function(a, b){ return d3.descending(a[" frequency"], b[" frequency"]); });

Note however that this doesn't entirely do what you expect: the frequencies end up in the order 94, 9, 9, 9, ..., 8, 8, 8, ..., etc. This is because they have been sorted as strings, not as numbers.但是请注意,这并不完全符合您的预期:频率以 94, 9, 9, 9, ..., 8, 8, 8, ... 等顺序结束。这是因为它们已经排序为字符串,而不是数字。

To deal with this either convert the values to numbers while sorting (note the extra + signs):要解决这个问题,要么在排序时将值转换为数字(注意额外的+符号):

    selected.sort(function(a, b){ return d3.descending(+a[" frequency"], +b[" frequency"]); });

Alternatively, you can convert the frequencies to numbers as part of reading in the files:或者,您可以将频率转换为数字作为读取文件的一部分:

function mapRow(row) {
    return { "words": row["words"], " frequency": +row[" frequency"] };
}

d3.csv("ArsenalDictionary.csv", mapRow, function(error1, Arsenal) {
    d3.csv("ChelseaDictionary.csv", mapRow, function(error2, Chelsea) {
        // ...

The former is more convenient but the latter may come in more useful if you want to do other things with the numbers, such as add up two counts if both files use the same word.前者更方便,但如果您想对数字做其他事情,后者可能更有用,例如如果两个文件使用相同的单词,则将两个计数相加。 ( world appears in both files). world出现在两个文件中)。

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

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