[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Reorder a XML file by distance from a location

Johnny Repp

6/23/2008 4:41:00 PM

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>
======
--
Posted via http://www.ruby-....

6 Answers

Joel VanderWerf

6/23/2008 9:12:00 PM

0

Johnny Repp wrote:
> 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'.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Johnny Repp

6/24/2008 2:53:00 AM

0


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

--
Posted via http://www.ruby-....

Mark Thomas

6/24/2008 2:06:00 PM

0


> 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

Johnny Repp

6/24/2008 4:26:00 PM

0


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

Very helpful, thank-you-very-much !
--
Posted via http://www.ruby-....

Johnny Repp

6/24/2008 7:46:00 PM

0

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?


--
Posted via http://www.ruby-....

Mark Thomas

6/24/2008 8:49:00 PM

0

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