Reorder a XML file by distance from a location

J

Johnny Repp

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"?>
<rss version="2.0">
<channel>
<title>something</title>
<description>myfeed</description>

<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.

</channel>
</rss>
======
 
J

Joel VanderWerf

Johnny said:
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"?>
<rss version="2.0">
<channel>
<title>something</title>
<description>myfeed</description>

<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.

</channel>
</rss>
======

There's nothing to sort on until you compute distance. Start by googling
for 'calculate distance from gps coordinates'.
 
J

Johnny Repp

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.
 
M

Mark Thomas

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
 
M

Mark Thomas

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top