From: Dave Howell Date: 2010-10-06T17:25:03+09:00 Subject: Re: Sorting problem with an Array of Arrays On Sep 22, 2010, at 21:30 , Paul wrote: >> 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. > > 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]] I'm not sure anybody really explained this well. arr.sort! do |a,b| #stuff to sort the array goes here end What that does is it puts one array element in a, and one in b. So, for example, you might have a = ["DEF-1", 1271772000, 1] and b = ["ABC-2", 1271773800, 1] So what you have to do in the "#stuff to sort the array goes here" is whatever code you need to have to figure out which one of those two comes first. It sounds like you need something like a[0].split("-")[0] to extract out the "DEF" or the "ABC" first, in order to sort by the label? Because a[0] is, of course, "DEF-1". sortorder = (a[0].split("-")[0] <=> b[0].split("-")[0]) if sortorder == 0 then a[1]<=>b[1] else sortorder end (You might need to look up what the <=> operator does...also, that's not how I would normally write that code. I was verbose for clarity's sake. ) If I understand your explanation correctly, then that code will NOT sort your array in the order you want, but it should help you understand how to use the .sort function to get there.