2

It seems the order of the filesystem entities returned by Pathname's .children method is arbitrary or at least not alphabetical.

Is there a way to have these returned in alphabetical order via the file system rather than calling .sort on the returned array?

Brendon Muir
  • 4,540
  • 2
  • 33
  • 55

1 Answers1

4

Pathname's children is actually doing:

def children(with_directory=true)
  with_directory = false if @path == '.'
  result = []
  Dir.foreach(@path) {|e|
    next if e == '.' || e == '..'
    if with_directory
      result << self.class.new(File.join(@path, e))
    else
      result << self.class.new(e)
    end
  }
  result
end

Dir.foreach calls the OS and iterates the directory passed in. There is no provision for telling the OS to sort by a particular order.

"What is the "directory order" of files in a directory (used by ls -U)?" is probably of interest to you.

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • While I appreciate the vote and all, please read the last paragraph in http://stackoverflow.com/help/someone-answers. – the Tin Man Apr 08 '16 at 17:27
  • I find that a strange policy. I've never had a mental link between an accepted answer and gratitude for that answer :) – Brendon Muir Apr 09 '16 at 08:19
  • It's not strange when you understand the goal of SO. It's not a discussion forum and chit-chat/back and forth via comments is discouraged because they're for clarification and recommendations. There are other mechanisms available for that. – the Tin Man Apr 09 '16 at 19:44