I am creating an app in which I need top 5 nearest riders location from cook location and then store in list an ascending order. I have found the nearest rider location from cook location. But i am bit confused how to add top 5 in list.
That's my code for finding nearest rider location.
try {
for(Location rlocation : rLocations){
float distance= cookerLOCATION.distanceTo(rlocation);
//int distance = Location.distanceBetween(cookLAT,cookLONG,rlocation.getLatitude(),rlocation.getLongitude(),results);
if(smallestDistance == -1 || distance < smallestDistance){
colsestRiderLocation = rlocation;
smallestDistance = distance;
comparingValues();
Log.d("Closet Rider Location",colsestRiderLocation.toString());
}
}
}catch (Exception e){
e.printStackTrace();
}
I think you just have to replace "smallestDistance" by an array of 5 and then just test every cases and you will have the closest one in the first element of the array and the far one ine the fifth element of the array :