问题
In my firebase database I have a collection of lat and long for each uid.

I use firebase recyler adapter data to list down my data to the recycler view.
FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class, R.layout.row, ViewHolder.class, mRef) {
@Override
protected void populateViewHolder(final ViewHolder viewHolder, final Model model, int position) {
Double dist = distance(
myPosition.latitude,
myPosition.longitude,
Double.parseDouble(model.getLat()),
Double.parseDouble(model.getLon()));
Log.d("gs_distance", String.valueOf(dist));
viewHolder.setDetails(getApplicationContext(),
model.getLat(),
model.getLon(),
model.getGsname());
}
The problem is I want to reorder/sort the data by nearest to farthest from my location, i can already fetch distance on how far each latlng is from my location. I dont know if there is a way to compare each latlng by their distance just before the data enters the populateViewHolder. Or maybe sort it after being inserted in the recyclerview? I really dont know.
static double distance(double fromLat, double fromLon, double toLat, double toLon) {
Location loc1 = new Location("");
loc1.setLatitude(fromLat);
loc1.setLongitude(fromLon);
Location loc2 = new Location("");
loc2.setLatitude(toLat);
loc2.setLongitude(toLon);
float distanceInMeters = loc1.distanceTo(loc2);
return distanceInMeters / 1000; //convert to km
}
来源:https://stackoverflow.com/questions/53308643/firebaserecycleradapter-sort-compare-reorder-data-before-populate