From: Paul Carvalho Date: 2009-08-31T00:20:11+09:00 Subject: Re: Sorting an array by multiple elements? Thanks for the great feedback! I couldn't find good documentation for the 'sort_by' and the examples provided didn't work with my data. It did nothing actually, which surprised me. Josh and Stefano's approach worked well enough. On Aug 30, 3:04 am, Stefano Crocco wrote: > This should do what you want. It first compares the names then, if they're > equal, it compares the dates. If the dates are also equal it compares the ids. > > summary_data.sort! do |a, b| >  res = a[3] <=> b[3] >  res = a[1] <=> b[1] if res == 0 >  res = a[0] <=> b[0] if res == 0 >  res > end > This gives me a 98% solution, which is good enough. There's a small catch or trick with my data that I can't work around. Details on the last 2%: Unfortunately, the data in the array is not a fixed size. Sometimes it is [ id, date, num, name ] and sometimes it may be [ id, date, num, name1, name2 ] (or more names.. I don't know how many since the data collection script figures it out as it goes along). When I do the sort by 'name' (e.g. first = a_name <=> b_name ), all the records with _additional_ names appears at the bottom of the list as if it were a different name (unexpected). For example, the output (now) looks like: [2, "2009-08-21", 2, "alfie"] [3, "2009-08-23", 3, "alfie"] [6, "2009-08-23", 3, "alfie"] [1, "2009-08-21", 4, "barny"] [5, "2009-08-24", 1, "cliff"] [4, "2009-08-21", 1, "cliff", "bob"] .. but I expect/want the output to look like: [2, "2009-08-21", 2, "alfie"] [3, "2009-08-23", 3, "alfie"] [6, "2009-08-23", 3, "alfie"] [1, "2009-08-21", 4, "barny"] [4, "2009-08-21", 1, "cliff", "bob"] [5, "2009-08-24", 1, "cliff"] Any thoughts? Cheers!