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();
}