0

我正在导入 iphone 日志的 csv 文件,并希望按日期对呼叫进行分组,并将每天的呼叫输出到 .txt 文件。

我正在导入:

src = <<csv
"name","Date","Duration","Call Type"
"Henry Jones ","2013-04-04 19:30:19","0:00:49","Outgoing"
"Alistair Morgan ","2013-04-05 13:14:07","0:00:03","Outgoing"
"Quentin Blah","2013-04-05 13:19:44","0:04:28","Missed"
"Quentin Blah","2013-04-05 13:25:19","0:09:45","Incoming"
"Henry Jones ","2013-04-05 14:35:29","0:00:24","Incoming"
"Henry Jones ","2013-04-05 15:54:53","0:00:00","Missed"
"Henry Jones ","2013-04-06 16:21:20","0:00:47","Outgoing"
"Bill and Ben Saunders ","2013-04-06 18:33:00","0:00:32","Outgoing"
"Phil Philly","2013-04-08 08:56:06","0:01:18","Outgoing"
"Barry Smith ","2013-04-08 13:51:03","0:00:48","Incoming"
"John Goole ","2013-04-08 18:24:57","0:06:28","Incoming"
"John Goole ","2013-04-08 18:32:07","0:00:35","Outgoing"
"Barry Barry","2013-04-08 18:33:29","0:01:56","Outgoing"
"Phil Michelson","2013-04-09 09:32:14","0:20:30","Outgoing"
"Nick Badson ","2013-04-09 12:00:28","0:00:00","Incoming"
"Henry Jones ","2013-04-09 15:09:02","0:00:00","Outgoing"
csv

require 'csv'
csv_data = CSV.parse (src)

headers = csv_data.shift.map {|i| i.to_s }
string_data = csv_data.map {|row| row.map {|cell| cell.to_s } }
array_of_hashes = string_data.map {|row| Hash[*headers.zip(row).flatten] }

p array_of_hashes

我现在正在努力知道如何对它们进行排序然后输出。

许多 TIA

4

1 回答 1

1

利用Enumerable#sort_by

尝试这个

array_of_hashes.sort_by{|h| h["Duration"]} #=> change "Duration" any key you needed.
于 2013-04-25T07:38:53.297 回答