When working with descending columns in #595 and #597 , I came across some descending-sort behavior that seems a little off.
Consider a set of data like this:
| Record | Value |
| --- | --- |
| A | 3 |
| B | 1 |
| C | - |
| D | - |
When sorting Value column in ascending order, the empty values are pushed to the bottom of the list:
| Record | Value |
| --- | --- |
| B | 1 |
| A | 3 |
| C | - |
| D | - |
But when sorting in descending order, the empty values are placed at the top of the list:
| Record | Value |
| --- | --- |
| C | - |
| D | - |
| A | 3 |
| B | 1 |
In some cases a user would want to "ignore" the empty values when sorting in descending order, and thereby push them to the bottom of the list, same as with ascending. Can this be done?
Did you see #529?
Oh, I see it now. Hm, maybe I can take a look at making a PR for that too.
Would be very welcome!
Hah, well, I may have talked myself out of the need for a PR. In my example case, the desired behavior can be achieved by use of the order_FOO()
method, and Django's nulls_last
parameter. Like so:
def order_value(self, QuerySet, is_descending):
if is_descending:
QuerySet = QuerySet.order_by(F('value').desc(nulls_last = True))
else:
QuerySet = QuerySet.order_by(F('value').asc(nulls_last = True))
return (QuerySet,True)
This results in null values always being sorted at the end of the column. This works for the cases I need it to, because I can ensure that my database always uses null for empty values.
I tested this solution for cases when the values are simply empty, and it does not work. I suspect a solution could be made by modifying something within django-tables2
order_by
behavior, but if we're talking about an edge case anyway, maybe the sort-with-nulls-last order_FOO()
workaround is better than messing with the source.
In any case, I can consider my request closed. If someone else clamors for treating strictly empty values in the same way, maybe a PR can be revisited.
Thanks for explaining!
thanks for this nice workaround @foldedpaper :)
Most helpful comment
Hah, well, I may have talked myself out of the need for a PR. In my example case, the desired behavior can be achieved by use of the
order_FOO()
method, and Django'snulls_last
parameter. Like so:def order_value(self, QuerySet, is_descending): if is_descending: QuerySet = QuerySet.order_by(F('value').desc(nulls_last = True)) else: QuerySet = QuerySet.order_by(F('value').asc(nulls_last = True)) return (QuerySet,True)
This results in null values always being sorted at the end of the column. This works for the cases I need it to, because I can ensure that my database always uses null for empty values.
I tested this solution for cases when the values are simply empty, and it does not work. I suspect a solution could be made by modifying something within
django-tables2
order_by
behavior, but if we're talking about an edge case anyway, maybe the sort-with-nulls-lastorder_FOO()
workaround is better than messing with the source.In any case, I can consider my request closed. If someone else clamors for treating strictly empty values in the same way, maybe a PR can be revisited.