Workaround broken SQL sort by logic