【发布时间】:2017-03-06 14:46:38
【问题描述】:
根据值对数组进行排序的最有效方法是什么。我有变量 user_location_lat 和一个 user_location_long 与数组 store_locations 一起。
我的变量用户位置有(例如)这个值:lat 45.222123123/long 54.5589858 数组看起来像这样:
id: 4, lat: 43.22243243, longi: 52,342234
id: 5, lat: 45.22243243, longi: 65,432432
id: 8, lat: 77.22243243, longi: 77,555324
etc.
我知道如何根据数组中的值对数组进行排序。但不是我如何根据数组外的值对它们进行排序。
如何从user_location 的数组中找到最接近的值?
编辑:是否有可能根据纬度和经度获得最近的位置?请参阅上面的编辑。
【问题讨论】:
-
您需要先遍历对象数组并计算到
user_location的距离并将其添加为对象的属性。然后sort()该属性的数组 -
如何存储与数组中的值相关的id?