I have a list of TimeSheet objects from an ealier AR query, which I
can sort by the full name of the user like so:
@time_sheet_entries.sort! { |a,b| a.user.full_name <=> b.user.full_name
}
I can sort by the started time like so:
@time_sheet_entries.sort! { |a,b| a.start_time <=> b.start_time }
My question is how do I do a sort so that the list is sorted primarily
by user.full_name but also by start_time? So that all the records for
Joe Blogs are together but within those rather than being random it is
then ordered by start_time?
I know I could do it by putting some SQL on the original AR query but
I’d like to know how to do it without doing that.
FYI what is happening is:
@time_sheet_entries.sort …
foreach entry in @time_sheet_entries
… print out that line in a table
end
I know that
@time_sheet_entries.sort!{ |a,b| a.user.full_name <=> b.full_name
}.sort!{ |a,b| a.start_time <=> b.start_time }
won’t do the trick because assuming it is even valid Ruby it would
just end up sorting by start_time. I thought that I could get a list
of the users then do a foreach loop just picking up the users then
iterating over them… (pseudo code so don’t expect this to actually
work…)
@time_sheet_entries.sort …
foreach user in @time_sheet_entries.unique(|x| x.user}
foreach entry in @time_sheet_entries.select(|x| x.user = user}
… print out that line in a table
end
end
but I think that’s quite lame. Surely there is a way to do it in one
line? Any ideas anyone ?
-Rob
“On two occasions, I have been asked [by members of Parliament],
‘Pray, Mr. Babbage, if you put into the machine wrong figures, will
the right answers come out?’ I am not able to rightly apprehend the
kind of confusion of ideas that could provoke such a question.”
– Charles Babbage (1791-1871)
“98.5% of DNA is considered to be junk DNA with no known purpose.
Maybe it’s XML tags.” – Anon