Working on an archive browser. I've run into a coding problem
--clearly I'm overlooking something. I can't seem to get the threaded
view to sort right. Here's the code:
def threaded
ts = sorted.group_by{ |msg| msg.subject }.to_a
ts = ts.sort_by{ |subject, messages| messages.max.date }
return ts
end
The #sorted method is providing an array of date-sorted Message
objects. I group by subject, transform that into an assoc array so I
can sort it again. Then I try to sort the threads by the most recent
of it's member messages. Seems logical but isn't working. Can anyone
see what I am missing?
Thanks.
On Sat, Jan 23, 2010 at 7:46 PM, Trans <transfire@gmail.com> wrote: > Working on an archive browser. I've run into a coding problem > --clearly I'm overlooking something. I can't seem to get the threaded > view to sort right. Here's the code: > > def threaded > ts = sorted.group_by{ |msg| msg.subject }.to_a > ts = ts.sort_by{ |subject, messages| messages.max.date } > return ts > end > > The #sorted method is providing an array of date-sorted Message > objects. I group by subject, transform that into an assoc array so I > can sort it again. Then I try to sort the threads by the most recent > of it's member messages. Seems logical but isn't working. Can anyone > see what I am missing? > What does #max normally uses #<=> to evaluate on the array's elements. How does that method behave on your message object? If the array of messages is already sorted by date, then just take messages.last. If it's not sorted, then make sure you're correctly "max"ing by date. > > Thanks. > -- mike dalessio mike@csa.net
On Sat, Jan 23, 2010 at 8:33 PM, Mike Dalessio <mike.dalessio@gmail.com> wrote: > What does #max normally uses #<=> to evaluate on the array's elements. How > does that method behave on your message object? def <=>(other) return -1 unless date return 1 unless other.date date <=> other.date end @ http://github.com/trans/librelive/blob/master/lib/librelive/message.rb > If the array of messages is already sorted by date, then just take > messages.last. If it's not sorted, then make sure you're correctly "max"ing > by date. Thanks. I'll try that.
On Sat, Jan 23, 2010 at 8:38 PM, Trans <transfire@gmail.com> wrote: > On Sat, Jan 23, 2010 at 8:33 PM, Mike Dalessio <mike.dalessio@gmail.com> wrote: > >> What does #max normally uses #<=> to evaluate on the array's elements. How >> does that method behave on your message object? > > def <=>(other) > return -1 unless date > return 1 unless other.date > date <=> other.date > end > > @ http://github.com/trans/librelive/blob/master/lib/librelive/message.rb > >> If the array of messages is already sorted by date, then just take >> messages.last. If it's not sorted, then make sure you're correctly "max"ing >> by date. > > Thanks. I'll try that. Well, it helped some. I switched to DateTime class and changed threaded to: def threaded ts = sorted.group_by{ |msg| msg.subject.sub(/^re\:\s*\[#{list}\]\s*/i, '') }.to_a ts = ts.map{ |subject, messages| [subject, messages.sort] } ts = ts.sort_by{ |subject, messages| messages.last.date } return ts end The member messages themselves are definitely sorting, but the threads as a whole still are not. One of those frustrating problems that should take about two minutes to solve, but s/two minutes/hours of racking head/ instead. Guess I'll try again tomorrow.