Search code examples
phplinuxwindowsfilesystemsglob

What is the "no sorting" order when referring to the GLOB_NOSORT flag


Every file-system directory menu/tree I have ever laid eyes on has always ordered files a-z with special characters either before or after (assumably in ASCII code order). I was looking at the GLOB_NOSORT flag and it is described as ...

GLOB_NOSORT - Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically

To me, files "appear in the directory" alphabetically anyway, so what is the no sorting order?

Is it ascii code order, filectime? filemtime or neither and does this differ depending on whether you are using window or linux

Also if its not obvious from your answer, how does this differ from "alphabetically".


Solution

  • Filesystems don't necessarily order files in any particular order. Even though the visual representation is sorted, since that's what users want, the actual contents on disk are not that.

    So when using this flag you are given whatever happens to be on the disk without sorting.

    Think what would happen if on every file delete/creation/move/rename the filesystem would take all the filenames in the directory, sort them based on possibly desired representation and then rewrite them. It would slow things down immensely.