# Sorting by computed temporary field

**URL:** <https://www.ruby-forum.com/t/sorting-by-computed-temporary-field/55493>\
**Category:** Rails\
**Created:** [March 21, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/sorting-by-computed-temporary-field/55493 "2006-03-21T17:47:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [March 21, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/sorting-by-computed-temporary-field/55493/1 "2006-03-21T17:47:50Z")

</div>

Hey all,

I’m rather new to Ruby and Rails (and not great with SQL), but I’m  
developing a ride sharing app and would like to be able to sort on  
something I don’t have stored.

I have users, events, and rides, each of which has a zip code. Events  
have\_many rides, and rides belong\_to events.

When an event is selected, I’d like to be able to show a list of rides  
to that event, sorted by distance from the logged-in user.

What are some ways to do that? Here’s what I can think of:

- Make a new find\_and\_sort method that adds a new @distance member  
element and sorts the new collection. Would something like this work?

def find\_and\_sort(event\_id)  
@rides = Ride.find(:all, :conditions =\> {“event\_id = ?”, event\_id})"  
for ride in @rides  
ride.newdistance = distance\_between\_zips(session[:user].zip\_code,  
Event.find(event\_id).zip\_code);  
end  
uhm… sort @rides by newdistance  
end

What if I want to also remove items from @rides before they are  
displayed if that newdistance is too large or some other condition?

uhm… I’m stumped, I guess. That’s why I’m here. 🙂

Many thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [March 21, 2006, 10:04pm UTC](https://www.ruby-forum.com/t/sorting-by-computed-temporary-field/55493/2 "2006-03-21T22:04:42Z")

</div>

Chris S. wrote:

> to that event, sorted by distance from the logged-in user.  
> Event.find(event\_id).zip\_code);  
> end  
> uhm… sort @rides by newdistance  
> end
> 
> What if I want to also remove items from @rides before they are  
> displayed if that newdistance is too large or some other condition?
> 
> uhm… I’m stumped, I guess. That’s why I’m here. 🙂

OK. Can’t do that.

But I should be able to overload \<=\>  
So in my ride model, could I do this?

def self.\<=\>(other)  
distance\_between\_zips(session[:user].zip\_code, self.zip\_code)  
\<=\> distance\_between\_zips(session[:user].zip\_code, other.zip\_code)  
end

Except… wow. Calling sort on the list means like n\*log(n) comparisons,  
and each compare calls the distance function twice, and each of those  
has five trig functions… so a list of 100 rides would take 2000 trig  
calls. Easy peasy!

Oh… do ActiveRecord objects mixin Comparable?

Comments please!

Chris S

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [March 21, 2006, 10:23pm UTC](https://www.ruby-forum.com/t/sorting-by-computed-temporary-field/55493/3 "2006-03-21T22:23:07Z")

</div>

Hello Chris !

2006/3/21, Chris S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > def find\_and\_sort(event\_id)  
> > @rides = Ride.find(:all, :conditions =\> {“event\_id = ?”, event\_id})"  
> > for ride in @rides  
> > ride.newdistance = distance\_between\_zips(session[:user].zip\_code,  
> > Event.find(event\_id).zip\_code);  
> > end  
> > uhm… sort @rides by newdistance  
> > end

@rides is an Array, and Array mixes in Enumerable. See #sort\_by:  
[http://www.ruby-doc.org/core/classes/Enumerable.html#M002092](http://www.ruby-doc.org/core/classes/Enumerable.html#M002092)

Hope that helps !
