我正在尝试根据距离找到排序列表。 (从最低到最高) 餐馆和顾客在数据库中有lat和lng。我从数据库中获取它们并比较它们并尝试显示最接近客户的餐馆列表。
这是我的代码
//ALGORITHOM
public ArrayList<String> displayNearestRestaurant(String customerId) throws SQLException{
//arraylist variable
ArrayList<String> restaurantList = new ArrayList<String>();
//array to store all distance
ArrayList<Map<String, String>> distanceList = new ArrayList<Map<String, String>>();
//grab customerId
Customer ob = new Customer();
//find lat and lng
String latStr =ob.getCustomerLat(customerId);
String lngStr =ob.getCustomerLng(customerId);
//convert to double
double customerLat = Double.parseDouble(latStr);
double customerLng = Double.parseDouble(lngStr);
//ok we found customer lat and lng
//now start loop and all restaurants available
con = new DBconnection().Connect(); //connects the db
Statement st = con.createStatement();
query = "SELECT * FROM restauranttable";
rs = st.executeQuery(query);
//starting loop
while (rs.next()) {
String restaurantId = rs.getString("restaurant_id");
String dbLatStr = rs.getString("lat");
double dbLat = Double.parseDouble(dbLatStr);
String dbLngStr = rs.getString("lng");
double dbLng = Double.parseDouble(dbLngStr);
//this.CalculateDistance(customerLat, customerLng, dbLat, dbLng, "M");
double dist = calculateDistance.distance(customerLat, customerLng, dbLat, dbLng, "M");
//double to string conversion
String strDist = Double.toString(dist);
// insert restaurantId and its distance against the customer position in a array
Map<String, String> distanceL = new HashMap<String, String>();
distanceL.put("restaurantId", restaurantId);
distanceL.put("distance",strDist);
distanceList.add(distanceL);
//I am lost at this point
}
con.close();
//now distanceList has all distance and restaurant id stored...
// now process the distanceList restaurant id and and value
//find add to the restaurant to the list which has smallest value
//now check the distanceL list
return restaurantList;
}
现在你可以看到distanceList被认为是持有餐馆ID和距离。我不知道如何对它们进行排序并根据最低到最高距离返回restaurantList。请帮我解决这个问题