# Help :: Sorting Dates

**URL:** <https://www.ruby-forum.com/t/help-sorting-dates/103883>\
**Category:** Rails\
**Created:** [May 11, 2007, 8:56am UTC](https://www.ruby-forum.com/t/help-sorting-dates/103883 "2007-05-11T08:56:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [May 11, 2007, 8:56am UTC](https://www.ruby-forum.com/t/help-sorting-dates/103883/1 "2007-05-11T08:56:02Z")

</div>

Hi all,  
I want to sort dates of form “Month year”.  
I’ve list of all dates in my database like , “Jan 2006” “Apr 2006” “Feb  
2006” “Dec 2005” …  
All are in as string. Is there any logic/method to sort them and  
display.  
I am stucked with this problem. Please Help me out.

Thanks,

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [May 11, 2007, 9:21am UTC](https://www.ruby-forum.com/t/help-sorting-dates/103883/2 "2007-05-11T09:21:25Z")

</div>

aveo wrote:

> Hi all,  
> I want to sort dates of form “Month year”.  
> I’ve list of all dates in my database like , “Jan 2006” “Apr 2006” “Feb  
> 2006” “Dec 2005” …

I got the solution and working…

g=‘Jan 2007’  
g1=‘Dec 2006’  
g2=‘Mar 2007’  
gd= Date.strptime(g, ‘%b %Y’)  
gd1= Date.strptime(g1, ‘%b %Y’)  
gd2= Date.strptime(g2, ‘%b %Y’)

puts [gd,gd1,gd2].sort  
puts gd.strftime(’%B %Y’)

===\>  
2006-12-01  
2007-01-01  
2007-03-01  
January 2007  
Thanks

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [May 11, 2007, 9:41am UTC](https://www.ruby-forum.com/t/help-sorting-dates/103883/3 "2007-05-11T09:41:01Z")

</div>

I got the solution and working…

> g=‘Jan 2007’  
> g1=‘Dec 2006’  
> g2=‘Mar 2007’  
> gd= Date.strptime(g, ‘%b %Y’)  
> gd1= Date.strptime(g1, ‘%b %Y’)  
> gd2= Date.strptime(g2, ‘%b %Y’)
> 
> puts [gd,gd1,gd2].sort  
> puts gd.strftime(’%B %Y’)
> 
> ===\>  
> 2006-12-01  
> 2007-01-01  
> 2007-03-01  
> January 2007

But how can use it for dynamic content like,

\<% @met.projects.each do |project| %\>  
  
\<%= project.duration\_from %\>  
----------- Here I want to collect all duration\_from from  
project table  
and get list of sorted duration\_from----------------  
\<% end %\>

Please help me out,  
Thanks,
