# Sort relative distance on the fly

**URL:** <https://www.ruby-forum.com/t/sort-relative-distance-on-the-fly/165763>\
**Category:** Rails\
**Created:** [April 17, 2009, 7:21am UTC](https://www.ruby-forum.com/t/sort-relative-distance-on-the-fly/165763 "2009-04-17T07:21:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [April 17, 2009, 7:21am UTC](https://www.ruby-forum.com/t/sort-relative-distance-on-the-fly/165763/1 "2009-04-17T07:21:33Z")

</div>

Hi I am quite a newbie in Ruby. I want to sort a list of locations by  
relative distance (shortest to farthest relative to my location) on the  
fly. For example, let say I am at x and y coordinate, I have other  
points in the map around me. I want to be able to list the other points  
in terms of the shortest distance from me to the farthest. I store  
longitude and latitude information on the database. However, I cannot  
store the relative distance permanently, as (i) number of points change  
in real time; and (ii) I don’t have relationship with the other points,  
which means the many-to-many tables are still empty.

Any ideas?

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [April 17, 2009, 7:29am UTC](https://www.ruby-forum.com/t/sort-relative-distance-on-the-fly/165763/2 "2009-04-17T07:29:25Z")

</div>

On Apr 16, 2009, at 10:21 PM, Milton Wong wrote:

> change  
> in real time; and (ii) I don’t have relationship with the other  
> points,  
> which means the many-to-many tables are still empty.
> 
> Any ideas?

Google for “earth distance sql”… or trust that the below is right 🙂

This assumes that some\_table has columns for latitude and longitude  
and that you’ve set variables for latitude, longitude and radius (in  
miles) to search on.

SELECT \* FROM some\_table WHERE  
(3960 \* 2 \* ASIN( SQRT( (1 - COS(RADIANS(latitude - #{latitude})))  
/ 2 + COS(RADIANS(#{latitude}))

- COS(RADIANS(latitude))
- (1 - COS(RADIANS(longitude - #{longitude}))) / 2))) \<= #{radius}

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [April 17, 2009, 6:30pm UTC](https://www.ruby-forum.com/t/sort-relative-distance-on-the-fly/165763/3 "2009-04-17T18:30:50Z")

</div>

Michael W. wrote:

> Hi I am quite a newbie in Ruby. I want to sort a list of locations by  
> relative distance (shortest to farthest relative to my location) on the  
> fly.  
> […]

No need to implement this yourself. Use PostgreSQL, PostGIS, and (if  
necessary) GeoRuby. This will make the task almost trivial.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [April 18, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/sort-relative-distance-on-the-fly/165763/4 "2009-04-18T21:02:43Z")

</div>

Philip H. wrote:

> On Apr 16, 2009, at 10:21 PM, Milton Wong wrote:  
> SELECT \* FROM some\_table WHERE  
> (3960 \* 2 \* ASIN( SQRT( (1 - COS(RADIANS(latitude - #{latitude})))  
> / 2 + COS(RADIANS(#{latitude}))
> 
> - COS(RADIANS(latitude))
> - (1 - COS(RADIANS(longitude - #{longitude}))) / 2))) \<= #{radius}

Thanks Phillip, Michael. I tried the formula and it works!
