I have a list of objects inserted in a Firestore Recycler and I need to sort them by the distance of the current user with the distance of the user from the object, I was able to calculate the distance between them by obtaining the current location of the user and pulling the location of the publisher of the object in question, it returns the distance in meters between them, and I wanted to use this as a parameter to order the data according to the value obtained, however, the only way to order the data obtained is using this document firestore queries Firestore
But it costs me dearly and is not very valid, is there anything I can do to change the order of the listed objects according to this local distance variable that I created?
Example
if(locationResult != null && locationResult.getLocations().size() > 0){
int latestLocationIndex = locationResult.getLocations().size() - 1;
latitudeUser = locationResult.getLocations().get(latestLocationIndex).getLatitude();
longitudeUser = locationResult.getLocations().get(latestLocationIndex).getLongitude();
localizacaoUser.setLatitude(latitudeUser); // Latitude do usuário adquirida
localizacaoUser.setLongitude(longitudeUser);
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
for (DocumentChange doc : documentSnapshots.getDocumentChanges()){
if (doc.getType() == DocumentChange.Type.ADDED){
AnuncioPrincipal anuncioPrincipal1 = doc.getDocument().toObject(AnuncioPrincipal.class);
Location localAnuncioAtual = new Location("ProviderAun");
localAnuncioAtual.setLatitude(anuncioPrincipal1.getLatAnun());
localAnuncioAtual.setLongitude(anuncioPrincipal1.getLongAnun());
Log.i("valor2848", "onEvent: "+localizacaoUser.distanceTo(localAnuncioAtual)/1000+" Km Used by" +
anuncioPrincipal1.getCidadeAnunciante()); // CityUser - Actual City
}
}
}
});
Basically I want to add an extra field that will be used to sort the data listed in the Recycler, but that is not in the Firestore, because if it is in the Firestore, each user will be changing this variable per minute.