The readdir method doesn’t guarantee any ordering. If you want to ensure they are sorted alphabetically you’ll need to do so yourself.
Note: I searched for a bit for definitive documentation saying this is the case. The closest I came is the following link
- http://utcc.utoronto.ca/~cks/space/blog/unix/ReaddirOrder
It’s by no means definitive but it does give a nice overview of the command, its history and how its implementation is typically traversal order.