Sorts directory entries from os.listdir()

Apparently listdir() is not required to return results in any particular
order.
Fixes file/directory collisions

A carefully crafted file could collide with a directory; this is no
longer possible.
Preserves internal links