I played with Date.strptime but it seems to ignore the time stamps.
I need to use the time stamps to sort the records correctly. My
current code joins the date and time and parses them together using
the Time library so that it can sort it based on date+time stamp.
Is there a way I can use the Date parser so that it factors in the date
+time?
(if there is, I haven't figured it out yet)
Thanks. Paul.
(I wish I could specify format for the Time parsing like I can with
Date parsing)
···
On Jun 7, 10:25 am, Robert Klemme wrote:
> The line that is breaking is:
>> data_array.sort! { |a,b| Time.parse( b[1]+' '+b[2] ) <=> Time.parse( a[1]+' '+a[2] ) }
>
> the data is an AoA and looks like:
> data_array = [ ['1', '04/14/2011', '10:00 am', 'foo'],
> ['2', '04/18/2011', '03:30 pm', 'bar'],
> ['3', '04/18/2011', '11:15 am', 'baz']]
> => so I expect the elements to resort as: 1, 3, 2.
I played with Date.strptime but it seems to ignore the time stamps.
Clearly, as Date is about _dates_.
I need to use the time stamps to sort the records correctly. My
current code joins the date and time and parses them together using
the Time library so that it can sort it based on date+time stamp.
Is there a way I can use the Date parser so that it factors in the date
+time?
(if there is, I haven't figured it out yet)
My bad. I overlooked that you want time as well. Then use class Time
or DateTime instead. They all have method strptime. Legal format
patterns are in the docs.
(I wish I could specify format for the Time parsing like I can with
Date parsing)
You can.
Btw, I'd rather convert strings on insertion into the Array. It's
usually best to convert input into specific types when one sees it.
Then you can use all the specific Date / Time / DateTime functionality
in your application and only convert back to string when you output
results. Also, often specific data needs less memory than data left
in strings. Also, converting only once is more efficient than doing
it over and over again. In your case you could as well use
Array#sort_by to convert each timestamp only once.
Kind regards
robert
···
On Tue, Jun 7, 2011 at 4:48 PM, Paul <tester.paul@gmail.com> wrote:
Hello Robert, I was thinking the same thing. The biggest difference is
between re-writing two sort routines and re-writing a third of the
entire program.
Looks like I have some refactoring to do this week..
Cheers! Paul.
···
On Jun 7, 10:58 am, Robert Klemme wrote:
Btw, I'd rather convert strings on insertion into the Array. It's
usually best to convert input into specific types when one sees it.
Then you can use all the specific Date / Time / DateTime functionality
in your application and only convert back to string when you output
results. Also, often specific data needs less memory than data left
in strings. Also, converting only once is more efficient than doing
it over and over again. In your case you could as well use
Array#sort_by to convert each timestamp only once.