Dc.js sorts data by multiple columns

I used dc.js to create a data table, and my requirement is to sort the data table in descending order of several columns. I used the following code:

.order(d3.descending)
     .sortBy(function (d) {
           return d.columnOne;
           return d.columnTwo;
});

Please, help.

+4
source share
2 answers

Your function that you pass to sortByshould return a single key that will sort the lines lexicographically or numerically in the order you want.

It is not possible to return two values ​​when you try to do this. Instead of this

  • if both columns are strings, join the values ​​as strings separated by a character with a lower ASCII value than any character in columnOne, or
  • , columnOne , columnTwo,

, :

.order(d3.descending)
     .sortBy(function (d) {
           return [d.columnOne,d.columnTwo].join('\x0');
});

,

.order(d3.descending)
     .sortBy(function (d) {
           return d.columnOne * 1e9 + d.columnTwo;
});

, - , .

, , , - .

+2

,

.sortBy(function (d) {
    return [d.columnOne,d.columnTwo].join();
});

. , !: -)

+1

All Articles