First lets assume you have all the LatLng points into a list. (received by the database even if they are not sorted or somehow)
Use this function to calculate between LatLng's points.
public double CalculationByDistance(LatLng StartP, LatLng EndP) {
int Radius = 6371;// radius of earth in Km
double lat1 = StartP.latitude;
double lat2 = EndP.latitude;
double lon1 = StartP.longitude;
double lon2 = EndP.longitude;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
int meterInDec = Integer.valueOf(newFormat.format(meter));
return Radius * c;
}
StartP should be the user's LatLng.
EndP should be all the other LatLngs
You could create a list inserting all the distances between the user's LatLng and other's LatLng, after that sort that list. Now, you have a sorted list with the distances between the user's LatLng and the rest LatLngs, so with a for loop you could do whatever you want to. If it's taking time you could add a progress bar informing the user that something is happening/loading etc.