What you have is a comma separated string of random elements. As you indicated the listagg function can sort the results. I think your best bet then is to parse the string into individual elements then let listagg rebuild the string with sorted elements. (See fiddle)
with test(str) as
( select 'M013,M007,M019,YYY,M018,XXX,999' from dual)
select listagg(estr,',') within group (order by estr)
from (select regexp_substr(str,'[^,]+', 1, level) estr
from test connect by regexp_substr(str, '[^,]+', 1, level) is not null
) ;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…