Does readdir() guarantee an order?
In short, no, readdir() does not guarantee any particular order.
from a readdir example in the glibc manual
The order in which files appear in a directory tends to be fairly random. A more useful program would sort the entries (perhaps by alphabetizing them) before printing them
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.