in reply to Re^4: Sort in multilevel hash
in thread Sort in multilevel hash

ORDER BY must be after WHERE (or GROUP BY or HAVING) of the last statement. You effectively have multiple statements thanks to the unions.