realm-kotlin icon indicating copy to clipboard operation
realm-kotlin copied to clipboard

Calling sort().first().find() doesn't work

Open ganfra opened this issue 2 years ago • 3 comments

Want to sort a list and retrieve the first one with sort().first().find() but it doesn't work. Using sort().find().firstOrNull() will work instead.

ganfra avatar Aug 05 '22 13:08 ganfra

@ganfra When you say it doesn't work, what do you mean exactly? Can you share an example?

cmelchior avatar Aug 05 '22 13:08 cmelchior

Yes sorry, I was not clear. So the result is not returned sorted as I'd have expected. But if I just call find().firstOrNull()instead it respects the sort operator.

Exemple:

class Task : RealmObject {
    @PrimaryKey
    var id: Long = 0,
    var name: String = ""
    var lastEditTs: Long = 0
}

If I want to get the latest edited task: realm.query(Task::class).sort("lastEditTs", Sort.DESCENDING).first().find() => will return the first edited realm.query(Task::class).sort("lastEditTs", Sort.DESCENDING).find().firstOrNull() => will return the last edited

ganfra avatar Aug 05 '22 13:08 ganfra

@ganfra Thanks for the clarification, we were able to reproduce the issue.

clementetb avatar Aug 08 '22 12:08 clementetb