I have an interesting issue with the below SELECT.
Its about ORDER BY clause; I am
Try to select the ORDER BY clause as a separate column using the DECODE() function:
SELECT DECODE(p_sortby, 'ID', gl.group_id, 'NAME', group_name) AS sort, ...
...
ORDER BY 1
Edit:
I'm not sure what you mean by "doesn't work". If you mean that member_count is not sorted as you expect, use TO_CHAR(gl.member_count, '000000') to enforce a formatted string conversion. (adjust format mask to expected number of digits)