ste9206
ste9206

Reputation: 1892

how to sort this custom RealmResult?

I need to do it to order my realm result in order of minimum distance. So I do it:

         public void onNext(final Location location)
        {
            latitude = location.getLatitude();
            longitude = location.getLongitude();

            nearbs = realm.where(Airport.class).greaterThanOrEqualTo("latitudeDeg",(latitude - 0.5)).lessThanOrEqualTo("latitudeDeg",(latitude + 0.5))
                    .greaterThanOrEqualTo("longitudeDeg",longitude - 0.5).lessThanOrEqualTo("longitudeDeg",longitude + 0.5).findAll();


            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm)
                {
                    for(Airport airport : nearbs)
                    {
                        Location location1 = new Location("");
                        location1.setLatitude(airport.getLatitudeDeg());
                        location1.setLongitude(airport.getLongitudeDeg());

                        float distance = location1.distanceTo(location);
                        distance = distance/1852; //Calculate the distance

                        airport.setDistance(distance);
                        Log.d("distance",String.valueOf(distance));

                        realm.insertOrUpdate(airport);
                    }

                    nearbs.sort("distance", Sort.ASCENDING);

                    adapter.updateNearbs(nearbs); //here there is notifyDataSetChanged()

                }
            });

        }

the problem is that Realm doesn't sort this list and so in adapter I don't have airport ordered by distance.

Is there a better way to do it? Thanks

Upvotes: 0

Views: 99

Answers (1)

Mohammed Atif
Mohammed Atif

Reputation: 4513

nearbs = nearbs.sort("distance", Sort.ASCENDING); 

in new updates of realm, you are supposed to assign sorted result to list

Upvotes: 1

Related Questions