Mark Thomas
6/24/2008 2:06:00 PM
> 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