-
Notifications
You must be signed in to change notification settings - Fork 185
more descending shorthand #1606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution. There are a variety of subtle considerations here.
if (typeof value === "string" && value?.startsWith("-")) { | ||
value = value.slice(1); | ||
order = reverseOrder(order); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If value is a string here, it is a field name rather than a reducer or a channel name. Because field names come from the user’s data, they are much more diverse than channel names and reducer names (which are generally fixed by Plot, though we do allow “extra” channels for the tip mark). This would break field names that start with a hyphen, which is admittedly unlikely and uncommon, but still possible. And since we aren’t going to allow x: "-foo"
for field names in general, this is somewhat inconsistent.
I don’t think we should introduce sort: "-foo"
shorthand for field names. There are already three ways to do this, and that seems reasonable enough:
sort: "foo", reverse: true
sort: {value: "foo", order: "descending"}
sort: (d) => -d.foo
(assuming foo is numeric)
We could introduce a value helper, perhaps: sort: Plot.negate("foo")
. This would take the same arguments that Plot.valueof takes for value, but would return the negative value. It would only work for numbers, though, of course…
Or perhaps we could also introduce a sort helper: sort: Plot.descending("foo")
which just returns {value: "foo", order: "descending"}
.
if (typeof sort === "string" && sort.startsWith("-")) { | ||
sort = sort.slice(1); | ||
reverse = !reverse; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A subtle gotcha here: sort: "-x"
doesn’t mean “sort by x in natural ascending order and then reverse the resulting order”; it means “sort by x in natural descending order”. The difference is that we want nulls/unordered values to be last when using -x, whereas reversing after sorting would put them first.
The ChannelDomainOptions interface is confusing in this regard because it calls the option reverse, but it really means whether to use ascending or descending order (per the documentation):
Lines 195 to 196 in 0490ba6
/** If true, use descending instead of ascending order. */ | |
reverse?: boolean; |
I commented on this indirectly in #1460, and I’m going to add some context to that issue now to clarify the confusion.
In any case, here we ideally don’t want to flip the reverse option; we want to use descending rather than ascending natural order. That’s why I added the reverseOrder helper for the basic sort transform:
Lines 143 to 149 in bef0ec9
function reverseOrder(order) { | |
return order === ascendingDefined | |
? descendingDefined | |
: order === descendingDefined | |
? ascendingDefined | |
: (i, j) => order(j, i); | |
} |
I think this might mean that the maybeSort helper should take an order argument, and that the bin and group transform should support order: "descending"
and order: "ascending"
as complements to the sort option, instead of only supporting a reverse option?
@@ -29,7 +29,7 @@ export interface GroupOutputOptions<T = Reducer> { | |||
* Plot.groupX({y: "count", sort: "count"}, {fill: "sex", x: "sport"}) | |||
* ``` | |||
*/ | |||
sort?: T | null; | |||
sort?: ReducerSort | null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is incorrectly narrowing the type of T
(e.g., in the case that T
is BinReducer
instead of Reducer
). We’ll need to find another way to type this.
if (typeof sort === "string" && sort.startsWith("-")) { | ||
sort = sort.slice(1); | ||
reverse = !reverse; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here re. reverse.
if (typeof order === "string" && order.startsWith("-")) { | ||
order = order.slice(1); | ||
reverse = !reverse; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here re. reverse. And also as commented above, order can be a field name, and I don’t think we should support -field syntax since it is more likely to conflict with a user-supplied column name.
bef0ec9
to
00338c8
Compare
I've struggled a bit with the types, but
I think the feature is complete(EDIT: well, no).