# Reorder a XML file by distance from a location

**URL:** <https://www.ruby-forum.com/t/reorder-a-xml-file-by-distance-from-a-location/141214>\
**Category:** Ruby\
**Created:** [June 23, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/reorder-a-xml-file-by-distance-from-a-location/141214 "2008-06-23T18:42:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnyrepp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@johnnyrepp](https://www.ruby-forum.com/u/johnnyrepp)\
**Post date:** [June 23, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/reorder-a-xml-file-by-distance-from-a-location/141214/1 "2008-06-23T18:42:47Z")

</div>

Hi

I’m quite the newbie and I’m looking for some help. I need to do  
something quick and it doesnt look so hard.

I have this RSS file on my computer (below) and I need to produce a very  
similar RSS file but with the items ordered according to the distance  
from a given location using the latitude and longitude. I’m trying to  
use rexml, but I can’t really get anything good. I don’t figure how  
sorting works.

Can someone give me some hints or advice?

======

\<?xml version="1.0"?\> something myfeed

```
  <item>
     <title>somewhere</title>
     <link>http://link</link>
     <description>niceplace</description>
    <geo:lat>45.6743546527171</geo:lat>
     <geo:long>130.7628651374888</geo:long>
  </item>

```

etc.

#

---

<div class="post-metadata">

**Author:** ![johnnyrepp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@johnnyrepp](https://www.ruby-forum.com/u/johnnyrepp)\
**Post date:** [June 23, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/reorder-a-xml-file-by-distance-from-a-location/141214/2 "2008-06-23T23:15:17Z")

</div>

Johnny Repp wrote:

> ```
> <title>somewhere</title>
> 
> ```
> 
> ======  
> There’s nothing to sort on until you compute distance. Start by googling  
> for ‘calculate distance from gps coordinates’.

---

<div class="post-metadata">

**Author:** ![johnnyrepp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@johnnyrepp](https://www.ruby-forum.com/u/johnnyrepp)\
**Post date:** [June 24, 2008, 4:54am UTC](https://www.ruby-forum.com/t/reorder-a-xml-file-by-distance-from-a-location/141214/3 "2008-06-24T04:54:57Z")

</div>

> There’s nothing to sort on until you compute distance. Start by googling  
> for ‘calculate distance from gps coordinates’.

thanks, but my problem is not with the distance, but merely sorting  
stuff. If you tell me how to sort alphabetically, or by latitude or  
longitude, I would be very happy already.

---

<div class="post-metadata">

**Author:** ![johnnyrepp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@johnnyrepp](https://www.ruby-forum.com/u/johnnyrepp)\
**Post date:** [June 24, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/reorder-a-xml-file-by-distance-from-a-location/141214/4 "2008-06-24T16:10:32Z")

</div>

> thanks, but my problem is not with the distance, but merely sorting  
> stuff. If you tell me how to sort alphabetically, or by latitude or  
> longitude, I would be very happy already.

The easy way is to include Comparable in a class:

class Location  
include Comparable  
attr\_accessor :title, :desc, :link, :lat, :long, :distance

def \<=\>(other)  
distance \<=\> other.distance  
end

def to\_s  
title  
end  
end

With the above class, you’d be able to do stuff like this:

near = Location.new  
near.title = “Near”  
near.lat = 45.3  
near.long = 130.2  
near.distance = calculate\_distance(near.lat, near.long)

far = Location.new  
far.title = “Far”  
far.lat = 20.7  
far.long = 129.0  
far.distance = calculate\_distance(near.lat, near.long)

puts [far, near].sort # =\> Near, Far

---

<div class="post-metadata">

**Author:** ![johnnyrepp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@johnnyrepp](https://www.ruby-forum.com/u/johnnyrepp)\
**Post date:** [June 24, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/reorder-a-xml-file-by-distance-from-a-location/141214/5 "2008-06-24T18:27:38Z")

</div>

> The easy way is to include Comparable in a class:

Very helpful, thank-you-very-much !

---

<div class="post-metadata">

**Author:** ![johnnyrepp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@johnnyrepp](https://www.ruby-forum.com/u/johnnyrepp)\
**Post date:** [June 24, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/reorder-a-xml-file-by-distance-from-a-location/141214/6 "2008-06-24T21:47:56Z")

</div>

Johnny Repp wrote:

> > The easy way is to include Comparable in a class:
> 
> Very helpful, thank-you-very-much !

So I can compare and sort two items…

May I ask how can I sort the whole stuff?

---

<div class="post-metadata">

**Author:** ![johnnyrepp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@johnnyrepp](https://www.ruby-forum.com/u/johnnyrepp)\
**Post date:** [June 24, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/reorder-a-xml-file-by-distance-from-a-location/141214/7 "2008-06-24T22:50:32Z")

</div>

> So I can compare and sort two items…
> 
> May I ask how can I sort the whole stuff?

The solution works for any number of items. sort() uses \<=\> during the  
sorting process, so when you define \<=\> for your data, you can sort  
any number of items.
