how to sort subarray in ruby

Go To StackoverFlow.com

0

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

2012-04-03 22:46
by Tomas Klein
"by either time or by steps" does not make sense (consider expanding the explanation and/or input+results). Also, sort {|x,y| x <=> y} is better written as sort. However, I imagine you want to do something such as sort {|x,y| x[1] <=> y[1]} or use sort_by {|e| e[1]}, etc - NoName 2012-04-03 22:47


2

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
2012-04-04 00:08
by Andrew Grimm
a.sort_by {|e| e[1]} --Great, worked,the rest didn't produce the right results `****1**** 20120210 08:04:05,404 DEBUG MQReceiver - Receive message

****2**** 20120210 08:03:05,404 DEBUG MQReceiver - Allocated message

****4**** 20120210 08:04:00,404 DEBUG MQReceiver - Sent message - Tomas Klein 2012-04-04 21:10

thanks a lot -it took me 2 months top sort something like thi - Tomas Klein 2012-04-05 20:36
Ads