processwire-requests icon indicating copy to clipboard operation
processwire-requests copied to clipboard

Sort by some field but if that field is empty then use some other field instead

Open webaff opened this issue 8 years ago • 2 comments

Short description of the enhancement

Disclaimer: I'm filing this request on the advice of Robins S, a much more respected member of the PW community. English, SQL and GitHub are not my mother tongue.

Enhance the mechanics behind $pages->find(), sort and the underlying "order by" part of the SQL statement in a way that we can sort by some field but if that field is empty then use some other field instead. No idea how or if it could be done. One nice result would be that sorting by a language-alternate field would work as expected.

Current vs. suggested behavior

Current: You cannot "sort by some field but if that field is empty then use some other field instead". Suggested: You can "sort by some field but if that field is empty then use some other field instead".

Why would the enhancement be useful to users?

The concept of language-alternate fields could be extended to selectors. No need to do workarounds with additional (hidden) fields, see links below.

Optional: Screenshots/Links that demonstrate the enhancement

https://processwire.com/talk/topic/7421-selector-sort-by-2-fields/ https://processwire.com/talk/topic/16746-how-to-use-language-alternate-fields-inside-selectors/#comment-147590

webaff avatar Jul 18 '17 19:07 webaff

My understanding is that this request is for "OR" sorting, where you could do something like...

sort=date_field|another_date_field

...where "another_date_field" is substituted for "date_field" in the sorting if "date_field" is empty. I don't see a way that this could be done, but thought it wouldn't hurt to ask.

Toutouwai avatar Jul 19 '17 00:07 Toutouwai

@Toutouwai it’s actually a small change to the way the SQL query is built: https://github.com/JanRomero/processwire/commit/eb49745a35e0b7896991fda69def9667e1347fcd

Of course, that’s just a cute demonstration… Much more work would be required to make this an official feature, and apparently it would be a breaking change because the pipe syntax is currently supported with a different meaning. My understanding is that sort=date_field|another_date_field is currently equivalent to sort=date_field, sort=another_date_field, both translating to order by date_field asc, another_date_field asc.

JanRomero avatar Feb 02 '23 12:02 JanRomero