Hi, Everyone I am a newbie to ruby....
just started with some examples
i have array
date = ['12/09/2007','06/06/2004','10/06/2005']
This is not an array of dates - it's an array of strings.
i wanted to sort dates in ascending order i.e
['06/06/2004','10/06/2005','12/09/2007']
tried using
p data.sort
could any one explain me whether there is built in class or need to
break tha date string
If these are dates you should use Date.
irb(main):005:0> date = ['12/09/2007','06/06/2004','10/06/2005']
=> ["12/09/2007", "06/06/2004", "10/06/2005"]
irb(main):006:0> real = date.map {|s| Date.parse s}
=> [#<Date: 4908887/2,0,2299161>, #<Date: 4906325/2,0,2299161>,
#<Date: 4907299/2,0,2299161>]
irb(main):007:0> puts real.sort
2004-06-06
2005-10-06
2007-12-09
=> nil
If for some serious reason you cannot use Date you should at least sort by Date:
irb(main):009:0> puts date.sort_by {|s| Date.parse s}
06/06/2004
10/06/2005
12/09/2007
=> nil
irb(main):010:0>
You need to require 'date' for this to work.
Kind regards
robert
···
2008/10/30 Chandu Chandu <chandu_750@yahoo.com>:
--
remember.guy do |as, often| as.you_can - without end