Ruby: 按 foat 字段对数组排序
Ruby: Sort array by a foat field
我有这个代码:
response = Place.all
nearPlaces = []
for el in response
distance = el.distance_to([41.9097306,12.2558141])
if distance < 20
el.distance = distance
nearPlaces.push(el)
end
end
orderPlaces = nearPlaces.sort_by{|e| e[:distance]}
但是数组不是按距离排序的...有什么建议吗?
nearPlaces.sort_by(&:distance)
我有这个代码:
response = Place.all
nearPlaces = []
for el in response
distance = el.distance_to([41.9097306,12.2558141])
if distance < 20
el.distance = distance
nearPlaces.push(el)
end
end
orderPlaces = nearPlaces.sort_by{|e| e[:distance]}
但是数组不是按距离排序的...有什么建议吗?
nearPlaces.sort_by(&:distance)