Help :: Sorting Dates

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,

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

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,

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs