14

Ruby docs don't mention this.

Does Dir.glob("*") guarantee the order of the files it returns, or do I need to sort them myself to be certain?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ali Lown
  • 2,323
  • 1
  • 18
  • 22

2 Answers2

9

No. Not if you're moving across OSes: http://rubyforge.org/tracker/index.php?func=detail&aid=12795&group_id=426&atid=1698

Tim Snowhite
  • 3,736
  • 2
  • 23
  • 27
  • 1
    And additionally I think, there is never a real internal ordering in the lower level, so every directory listing output is pre-sorted in a special way; harddisks/filesystems have there own file/dir organization which is in general not alphabetical. That means: If you need sorted listing -> sort it! – asaaki Jun 02 '11 at 23:28
  • 1
    @asaaki, sometimes there *is* low-level ordering. For example, NTFS stores directory entries in raw, binary Unicode order (ordinal sort). See Raymond Chen's post: ["Why do NTFS and Explorer disagree on filename sorting?"](http://blogs.msdn.com/b/oldnewthing/archive/2005/06/17/430194.aspx). He also points out that the particular ordering used is not appropriate for the user, so you'd still need to sort for the UI. – efotinis Nov 06 '11 at 08:45
1

If the docs don't say it, then there is no guarantee, and you need to sort it for yourself. Even if it works in your current ruby, it may not in your next.

Mori
  • 27,279
  • 10
  • 68
  • 73