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.
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.
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
The easy way is to include Comparable in a class:
Very helpful, thank-you-very-much !
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?
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.