Forum: Ruby on Rails sort-weirdness - active record issue?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Larry W. (Guest)
on 2006-03-09 17:39
(Received via mailing list)
I'm selecting data into a controller and sorting it.  The code (in the
controller) looks like this:

def index
        @assignments = []
        tasks =   Task.find(:all, :conditions => ["person_id = ? and
actual_end_date is null", session[:user].id.to_s ])
        issues =  Issue.find(:all, :conditions => ["person_id = ? and
<> 'closed'" , session[:user].id.to_s] )
        @assignments= @assignments.sort
        @assignments.each {|ea| puts +

The output from the puts statement is this:
P2 Task1      2006-03-16
T2                2006-03-23
Test Task     2006-01-05
super tests   2006-01-19
super test 3  2006-01-26
Larry's issue 2006-03-09

For some reason, the sort algorithm thinks that all the dates are nil so
they don't get sorted, but they're there as the puts shows. The sort
is below.  When i didn't include the check for nil in it, it threw an
exception, saying nil doesn't understand <=>.

    # Sort by due date (scheduled_end_date)
    def <=> aTask
          @scheduled_end_date.nil? ? -1 : @scheduled_end_date <=>

Any ideas would be greatly appreciated.
Lugovoi N. (Guest)
on 2006-03-09 17:54
(Received via mailing list)
@assignments= @assignments.sort  {|a,b| a.scheduled_end_date <=>
Matthias von Rohr (Guest)
on 2006-03-09 18:00
(Received via mailing list)
Hi Larry,

Try this:

tasks = Task.find_by_person_id session[:user].id, :order =>
'scheduled_end_date DESC'  # or ASC for ascending

Hope that helps (you may want to fire up the console using 'script/
console' and try out diffrent queries)

Larry W. (Guest)
on 2006-03-09 18:03
(Received via mailing list)
that works. Thank you.

But Why? I thought the two approaches were equivilent.
Larry W. (Guest)
on 2006-03-09 18:06
(Received via mailing list)
I need to concatenate the results from two tables before sorting, so
doesn't work.
This topic is locked and can not be replied to.