Realm findAllSorted by a field of a field

Fer*_*ano 3 java android realm

关于境界的另一个问题.

我有这个结构;

A类有一个B类,它有一个String名称.

我想用B b对A类列表进行排序,其名称为"xy";

所以这就是我尝试但不起作用的方式.

realm.where(A.class).findAllSorted("b.name",true);
Run Code Online (Sandbox Code Playgroud)

这表示没有字段B.name.

任何想法我怎么能让它有效?

谢谢.

bee*_*der 5

Realm不支持按链接排序.有一个未解决的问题跟踪此问题.

以下是Realm支持该功能之前的解决方法:

class A extends RealmObject {
    private B b;
    // Storing the b.name as a field of A when calling setB(). But
    // remember you cannot do it by adding logic to setB() since Realm's
    // proxy will override the setters. You can add a static method to
    // achieve that.
    private String bName;

    // getters and setters

    // This needs to be called in a transaction.
    public static void setBObj(A a, B b) {
        a.setB(b);
        a.setBName(b.getName);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以按bName对结果进行排序,如: realm.where(A.class).findAllSorted("bName",true);