0

I'm trying to sort and then slice a set of javascript objects. I'm successfully slicing the data, but the sort is returning a list of categories that is neither alphabetically sorted by category nor by value.

My CSV looks like this:

category,date_calc,value,YYYY,MM
string1,2011-08-01,46440.16,2011,8
string2,2013-03-01,68249.72,2013,3
string1,2014-01-01,4285,2014,1
string3,2012-03-01,47646.82,2012,3
...

This is my code:

d3.csv("data/ncc_category.csv", function(error, csv) {   
    var data = d3.nest()
    .key(function(d) { return d.category;})
    .rollup(function(d) { return d3.sum(d, function(g) {return g.value; }); }).entries(csv) 

    var sorted = data.sort(function (a, b) {
        return a.value - b.value; 
    }).reverse();

    var top10 = sorted.slice(0, 10); // slice
        console.log(top10);
});

EDIT: Screen grab for the first 4 objects in data.

Console output for data:

Screen grab for the first 4 objects in top10

Console output for top10

woodbine
  • 553
  • 6
  • 26
  • Make sure `data` contains what you think it contains (with a `console.dir(data);` for instance). Also you can remove the `.reverse()` by switching `a` and `b`. – sebnukem May 20 '15 at 20:27
  • `data` is fine and rolling up correctly, its just the sorting isn't working, as if it isn't registering `value` in `b.value - a.value;` - thanks for the help with `reverse`. – woodbine May 20 '15 at 20:37
  • You probably want to parse `value` into a number (e.g. by putting a `+` in front of it). – Lars Kotthoff May 20 '15 at 20:53
  • Hi Lars, sorry to be ignorant, but where would you do that? I've tried a couple of options including `return +g.value` but can't seem to make it work. – woodbine May 20 '15 at 21:04
  • `return +b.value - +a.value;`. its a quick way to turn a string into a digit – PhilVarg May 20 '15 at 21:12
  • Thanks Phil, annoyingly that's had no effect. – woodbine May 20 '15 at 21:17
  • Can you post what `data` (part of it) to see what it looks like? – sebnukem May 20 '15 at 21:57

1 Answers1

1

Ok, so this is a salutary lesson in column naming, having named my column value, I didn't pick up on the fact that when d3 constructs a rolled up object array, it gives the primary column the name key and the summed column the name values. Once I changed the column name in the original csv I could see the problem, I also used the sorting code suggested here: Sort array of objects by string property value in JavaScript

d3.csv("data/ncc_category.csv", function(error, csv) {   
var data = d3.nest()
.key(function(d) { return d.category;})
.rollup(function(d) { return d3.sum(d, function(g) {return +g.total_spend; }); }).entries(csv)  

var sorted = data.sort(function (a, b) {
    if (a.values < b.values) {
        return 1;
}
    if (a.values > b.values) {
        return -1;
    }
    // a must be equal to b
    return 0;
});
console.dir(sorted);

var top10 = sorted.slice(0, 10); // slice the first 10
    console.dir(top10);
});
Community
  • 1
  • 1
woodbine
  • 553
  • 6
  • 26