i need to sort the following ,any ideas are appreciated:
a=[
["****************1************","20120210 08:04:05,404 DEBUG MQReceiver - Receive message "<FIXML>","\n"],
["****************3************","20120210 08:04:00,404 DEBUG MQReceiver - Sent message "<FIXML>","\n"],
["****************2************","20120210 08:03:05,404 DEBUG MQReceiver - Allocated message "<FIXML>","\n"],
]
how to sort this array either by time or by steps 1,2,3-usual a.sort{|x,y| x<=>y} not working here
If you're trying to sort by two criteria, do
a.sort_by do |item|
time = parse_time_from_string(item[1])
step = parse_step_from_string(item[0])
[time, step]
end
****2****
20120210 08:03:05,404 DEBUG MQReceiver - Allocated message
****4****
20120210 08:04:00,404 DEBUG MQReceiver - Sent message
sort {|x,y| x <=> y}
is better written assort
. However, I imagine you want to do something such assort {|x,y| x[1] <=> y[1]}
or usesort_by {|e| e[1]}
, etc - NoName 2012-04-03 22:47