# Order some files by time before display

**URL:** <https://www.ruby-forum.com/t/order-some-files-by-time-before-display/147128>\
**Category:** Ruby\
**Created:** [August 28, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/order-some-files-by-time-before-display/147128 "2008-08-28T17:54:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [August 28, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/order-some-files-by-time-before-display/147128/1 "2008-08-28T17:54:35Z")

</div>

Hi everybody,

I have write a short script that display each article of a folder:

require ‘find’  
result = []  
@pattern = ‘\*.html’  
Find.find(‘public/2008/’) do |p|  
next unless File.file?§  
name = File.basename§  
result \<\< p if File.fnmatch(@pattern, name)  
end  
a = ‘’  
result.length.times do |i|  
content = File.read(result[i])  
a += ‘

’  
a += content  
a += ‘’ + File.ctime(result[i]).to\_s + ‘’  
a += ‘
’  
end  
a

As you can see, it display some articles in a webpage. However, but I  
would like to order them by time (with File.ctime(result[i]) for  
instance) such as in blogs. If you see a sexy way to do so, that would  
be very helpful for me.

Thanks,  
Dendicus

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [August 28, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/order-some-files-by-time-before-display/147128/2 "2008-08-28T18:24:37Z")

</div>

Hi David,

David B. wrote:

> require ‘find’  
> result = []  
> @pattern = ‘\*.html’  
> Find.find(‘public/2008/’) do |p|  
> next unless File.file?§  
> name = File.basename§  
> result \<\< p if File.fnmatch(@pattern, name)  
> end

No need for ‘find’. A simple ‘glob’ suffices:

files = Dir[File.join(‘public/2008/’, ‘\*\*’, @pattern)]

David B. wrote:

> a = ‘’  
> result.length.times do |i|  
> content = File.read(result[i])  
> a += ‘
> 
> ’  
> a += content  
> a += ‘’ + File.ctime(result[i]).to\_s + ‘’  
> a += ‘
> ’  
> end  
> a

Iterating over an array like this is kind of cumbersome. ‘each’ should  
be your weapon of choice here. Furthermore using the \<\< operator for  
concatenation could yield in better performance as += constructs a new  
String object whereas \<\< doesn’t (AFAIK).

David B. wrote:

> As you can see, it display some articles in a webpage. However, but I  
> would like to order them by time (with File.ctime(result[i]) for  
> instance) such as in blogs. If you see a sexy way to do so, that would  
> be very helpful for me.

Hope that’s sexy enough for you:

result = result.sort\_by { |p| File.ctime§ }

To sum it all up here is how I would have done it:

* * *

@dir = ‘public/2008’  
@pattern = ‘\*.html’

files = Dir[File.join(@dir, ‘\*\*’, @pattern)].select { |f| File.file?(f)  
}  
files = files.sort\_by { |f| File.ctime(f) }

## a = ‘’ files.each do |f| a \<\< ‘ ’ a \<\< File.read(f) a \<\< ‘’ + File.ctime(f).to\_s + ‘’ a \<\< ‘ ’ end a

Hope that helps!

Regards,  
Matthias

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [August 28, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/order-some-files-by-time-before-display/147128/3 "2008-08-28T18:57:59Z")

</div>

Many thanks, Matthias! Your answer was perfectly resolve my problem and  
now the code is really more fashion!

Thanks again,  
Dendicus
