Might try throwing everything in that loop into an array first... then sort the array by alphabeticall order then use another loop to create your html from the array:
You could sort like :
sort ($directoryarray);
reset ($directoryarray);
see the manual entry for Sort for more details 🙂