add sort criteria to columns joined to query

https://community.openproject.org/work_packages/17814

Additionally, it fixes the last query referenced in 

https://community.openproject.org/work_packages/17750