# Sort-weirdness - active record issue?

**URL:** <https://www.ruby-forum.com/t/sort-weirdness-active-record-issue/78995>\
**Category:** Rails\
**Created:** [March 9, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/sort-weirdness-active-record-issue/78995 "2006-03-09T16:39:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 9, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/sort-weirdness-active-record-issue/78995/1 "2006-03-09T16:39:08Z")

</div>

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  
status  
\<\> ‘closed’” , session[:user].id.to\_s] )  
@assignments.concat(tasks)  
@assignments.concat(issues)  
@assignments= @assignments.sort  
@assignments.each {|ea| puts ea.name +  
ea.scheduled\_end\_date.to\_s}  
end

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  
method  
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 <=>

```

aTask.scheduled\_end\_date  
end

Any ideas would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 9, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/sort-weirdness-active-record-issue/78995/2 "2006-03-09T16:54:14Z")

</div>

@assignments= @assignments.sort {|a,b| a.scheduled\_end\_date \<=\>  
b.scheduled\_end\_date}

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 9, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/sort-weirdness-active-record-issue/78995/3 "2006-03-09T17:00:19Z")

</div>

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)

Matt

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 9, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/sort-weirdness-active-record-issue/78995/4 "2006-03-09T17:03:24Z")

</div>

that works. Thank you.

But Why? I thought the two approaches were equivilent.

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 9, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/sort-weirdness-active-record-issue/78995/5 "2006-03-09T17:06:30Z")

</div>

I need to concatenate the results from two tables before sorting, so  
this  
doesn’t work.
