dc-js / dc.js

Multi-Dimensional charting built to work natively with crossfilter rendered with d3.js
Apache License 2.0
7.42k stars 1.8k forks source link

expose ordering predicate #1658

Open gordonwoodhull opened 4 years ago

gordonwoodhull commented 4 years ago

Now that we are using built-in Array.sort, we could expose an ordering predicate instead of only .ordering().

.ordering() makes it difficult to sort descending, for example.

This replaces #1161, which I overexplained and made too complicated.

gordonwoodhull commented 4 years ago

If the predicate is specified, it would have precedence over the ordering accessor.

Currently we have

https://github.com/dc-js/dc.js/blob/92c897a47acc333c2c11fd1f2ca7ca69c0886429/src/base/base-mixin.js#L363-L366

This could be

 _computeOrderedGroups (data) { 
     // clone the array before sorting, otherwise Array.sort sorts in-place
     const defaultPredicate = (a, b) => this._ordering(a) - this._ordering(b); 
     return Array.from(data).sort(_orderPredicate || defaultPredicate); 
 } 

and then add a getter/setter orderPredicate.

cwolcott commented 4 years ago

How you thought anymore about implementing something like this. I have a mixed case of keys and would like to sort by case insensitive order.

An example of my keys are: all, Anyone, lightning, Thunder

The order in the html-legend is: Anyone, Thunder, all, lightning

gordonwoodhull commented 4 years ago

It should be easy - want to give it a shot?

Case insensitivity would be an excellent test case.

Just don’t read #1161 - that made it sound much too complicated. It should just be the one line change above.