
Untitled
By: a guest on
Apr 29th, 2012 | syntax:
None | size: 0.73 KB | hits: 8 | expires: Never
splitting an array of strings which contain dates into date order
["09/Feb/2012:12:56:40.009+0000 13894 10.0.0.1",
"09/Feb/2012:14:45:03.829+0000 12951 10.0.0.1",
"09/Feb/2012:15:13:07.722+0000 3812 10.0.0.1",
"09/Feb/2012:15:18:47.813+0000 50290 10.0.0.1",
"09/Feb/2012:15:18:55.430+0000 2796 10.0.0.1",
"09/Feb/2012:16:02:13.494+0000 5193 10.0.0.1",
"09/Feb/2012:16:17:18.661+0000 4523 10.0.0.1",
"09/Feb/2012:16:59:50.671+0000 9764 10.0.0.1",
"09/Feb/2012:17:07:55.129+0000 3944 10.0.0.1"]
require 'date'
a.max_by{|e| DateTime.strptime(e.split(' ').first, '%d/%b/%Y:%H:%M:%S.%L%z')}
#=> "09/Feb/2012:17:07:55.129+0000 3944 10.0.0.1"
values = #your array of strings
latest_date = values.map{|d| d.split(' ')[0]}.sort.last