如何对这个自定义RealmResult进行排序?

时间:2017-01-18 13:20:55

标签: android realm

我需要按照最小距离的顺序来命令我的领域结果。所以我这样做:

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

                }
            });

        }

问题是Realm没有对此列表进行排序,因此在适配器中我没有按距离排序机场。

有更好的方法吗? 感谢

1 个答案:

答案 0 :(得分:1)

Ryans-MacBook-Pro:~ ryan$ python
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:52:12) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

在领域的新更新中,您应该将已排序的结果分配给列表