sorter.sort_by{|name, user_id| user_id} is not working. Even though the hash map sorter is sorted it prints unsorted hash.
I have append the name and user_id with the required condition in the hash map:
require 'json'
class Numeric
def to_rad
self * Math::PI / 180
end
end
def distance( lat2, lon2)
lat1=12.9611159
lon1=77.6362214
dLat = (lat2-lat1).to_rad;
dLon = (lon2-lon1).to_rad;
a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.to_rad) * Math.cos(lat2.to_rad) *
Math.sin(dLon/2) * Math.sin(dLon/2);
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
d = 6371 * c;
return d
end
file= File.read('customers.json')
data_hash= JSON.parse( file)
sorter = Hash.new
data_hash["customers"].each do |user|
latitude=user["latitude"].to_f
longitude=user["longitude"].to_f
$count=0
if (distance(latitude,longitude).to_f < 100.00)
name = user["name"]
user_id=user["user_id"]
sorter[name]=user_id
# print name," ",user_id
# print "\n"
end
#
end
sorter.sort_by{|name, user_id| user_id}
print sorter