From: "Jesús Gabriel y Galán" Date: 2010-09-23T16:22:24+09:00 Subject: Re: Sorting problem with an Array of Arrays On Thu, Sep 23, 2010 at 6:30 AM, Paul wrote: > Hi Jeremy, thanks for the reply. > > On Sep 22, 6:54 pm, Jeremy Bopp wrote: > >> > You could define a class to represent each of your inner array elements. >> >  That class could then define the <=> method in which you could compare >> > each item in the order you desire.  Then your outer array, consisting of >> > instances of your class, could be trivially sorted with Array#sort. > > I'm afraid I don't understand what you are saying here.  All I > understood was "could be trivially sorted with Array#sort."  I don't > understand classes. > > >> Silly me.  You can also pass a block to the Array#sort method.  That >> block will receive 2 arguments which you may then compare as you wish, >> returning -1 if the first argument sorts before the last, 0 if they are >> equal, and 1 if the last argument sorts before the first. >> >> -Jeremy > > Do you have an example of what this might look like?  To clarify what > I want to see, here is how the data should be sorted: > > [["ABC-1", 1271768400, 2], >  ["DEF-1", 1271772000, 1], >  ["ABC-2", 1271773800, 1], >  ["AAA-1", 1271862000, 2], >  ["ABC-3", 1271863200, 2], >  ["ABC-4", 1271869200, 2]] Does this mean the array has to be sorted based on timestamps (i.e: the second element of each subarray)? If this is so, then this works: irb(main):004:0> arr1.sort_by {|x| x[1]} => [["ABC-1", 1271768400, 2], ["DEF-1", 1271772000, 1], ["ABC-2", 1271773800, 1], ["AAA-1", 1271862000, 2], ["ABC-3", 1271863200, 2], ["ABC-4", 1271869200, 2]] > When sorted correctly, I can see the data input problem with the > 'ABC-3' time stamp.  The label sort is important except when the > labels change, then they need to be sorted numerically. I don't understand this. What do you mean by "when the labels change?" Jesus.