I have something that I wonder if I can do better. I have a list or records, employee records
Marco from Amsterdam Silvia from Amsterdam Enola from Rotterdam Elisa From Utrecht
These are now shown as
Amsterdam Marco Amsterdam Silvia Rotterdam Enola Utrecht Elisa
What I would really want is
Amsterdam Marco Silvia
Rotterdam Enola
Utrecht Elisa
So to have them grouped by the City from which they come from Is there an elegant solution to this? I know a couple, but all seem like nasty hacks (like putting all the cities up and query each and every city)
Assuming that each record has name and city as attributes you could do something like:
last_city = nil @results.each do |r| if last_city != r.city then puts "\n" unless r == @results.first puts r.city last_city = r.city end puts r.name end
My memory is there's a nifty way to do it in Ruby, but I can't recall what it is right now...