From: Jonathan Leighton Date: 2006-01-31T05:16:10+09:00 Subject: Re: Loop weirdness On Tue, 2006-01-31 at 05:03 +0900, Robert Klemme wrote: > 2006/1/30, Jonathan Leighton : > > On Mon, 2006-01-30 at 05:33 +0900, Simon Kræ—¦ger wrote: > > > Jonathan Leighton wrote: > > > > > > > Hi, > > > > > > > > I'm going ever so slightly crazy over some looping behaviour. Here's a > > > > simplified test case I made: > > > > > > Is this what you are trying to do? > > > > > > def categorise(items) > > > items.inject(Hash.new([])) do |h, item| > > > h[item[:num]] += [item[:foo]]; h > > > end.map{|k, v| {:num => k, :items => v}} > > > end > > > > That's cool (although crowded), but it doesn't fit the bill for me as I > > need the order to be retained. > > What exactly do you want to do? Can you describe it other than in > code? Input, output etc. I did, back in the thread a little: ---- These are interesting but unfortunately I need the order of the original items array to be maintained. This is because for the *actual* purpose, it's sorting a number of rows from a database and categorising them by a date field. The rows are returned in a certain order from the database and that order must be kept. ---- But don't worry about it, I am happy with the solution I've found. (That is, there is no longer a *problem*, although I'm always interested to see better/leaner/cleaner ways of doing things). Jon -- Jonathan Leighton http://turnipspatch.com/ | http://jonathanleighton.com/ | http://digital-proof.org/