# Re: Find.find --- returns directories/files backwards

**URL:** <https://www.ruby-forum.com/t/re-find-find-returns-directories-files-backwards/88409>\
**Category:** Ruby\
**Created:** [March 10, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/re-find-find-returns-directories-files-backwards/88409 "2007-03-10T16:55:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joseph\_Schiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba9def/32.png) [@Joseph\_Schiller](https://www.ruby-forum.com/u/Joseph_Schiller)\
**Post date:** [March 10, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/re-find-find-returns-directories-files-backwards/88409/1 "2007-03-10T16:55:04Z")

</div>

Hi,

Try something like this

d = Dir.open(‘/user/path/Documents’)  
d.entries.sort  
=\> [“.”, “…”, “doc1”, “doc2”, etc]

for some reason block processing is counter intuitive here  
this code does not work

e = Dir.open(‘/user/path/Documents’)  
e.entries.sort {|file| puts file }  
=\> warning: multiple values for a block parameter (2 for 1)

Regards,

joe

----- Original Message ----  
From: Brad [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Friday, March 9, 2007 7:35:05 PM  
Subject: Find.find — returns directories/files backwards

New user question:

It seems to me when I run:  
Find.find(‘/user/name/documents’) {|path| puts path}

it returns all the directories in the reverse order. I was expecting  
the directories to be returned in alphabetical order, but that doesn’t  
seem to be the case. Also, in one case it reads half of a directory’s  
files, then the sub dirs and then it finished reading the rest of the  
directory it started in and finished writing them in the Puts  
statement.

Am I missing something? How do you do get it to write out the  
directories in alphabetical order?

Any and all help welcome.

Thank you.

Brad

---

<div class="post-metadata">

**Author:** ![Joseph\_Schiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba9def/32.png) [@Joseph\_Schiller](https://www.ruby-forum.com/u/Joseph_Schiller)\
**Post date:** [March 10, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/re-find-find-returns-directories-files-backwards/88409/2 "2007-03-10T17:08:09Z")

</div>

Joseph Schiller wrote:

> for some reason block processing is counter intuitive here  
> this code does not work
> 
> e = Dir.open(’/user/path/Documents’)  
> e.entries.sort {|file| puts file }  
> =\> warning: multiple values for a block parameter (2 for 1)

It doesn’t work because you pass a block that accepts only one argument  
to  
sort. Any block passed to sort is supposed to take two arguments,  
compare  
these arguments and return -1, 0 or 1 depending on which argument is  
greater.

What you apparenty want to do would be archieved by writing  
e.entries.sort.each {|file| puts file }  
thus passing the block to each and not to sort.

---

<div class="post-metadata">

**Author:** ![Joseph\_Schiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba9def/32.png) [@Joseph\_Schiller](https://www.ruby-forum.com/u/Joseph_Schiller)\
**Post date:** [March 10, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/re-find-find-returns-directories-files-backwards/88409/3 "2007-03-10T17:31:12Z")

</div>

On 10.03.2007 17:07, Sebastian H. wrote:

> these arguments and return -1, 0 or 1 depending on which argument is greater.
> 
> What you apparenty want to do would be archieved by writing  
> e.entries.sort.each {|file| puts file }  
> thus passing the block to each and not to sort.

There is also a class method entries, so the above code can be rewritten  
as

Dir.entries(’/user/path/Documents’).sort.each {|e| puts e}

or even shorter

puts Dir.entries(’/user/path/Documents’).sort

Kind regards

robert
