Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm String greaterThan

Is there any way to find all (or just the next) RealmObjects with Strings lexicographically greater than the target?

Something like

MyEntry next = realm.where(MyEntry.class)
        .greaterThan("name", current)
        .findAllSorted("name")
        .first();

which did not work, because greaterThan is not implemented for Strings.

like image 750
serv-inc Avatar asked Sep 14 '26 22:09

serv-inc


1 Answers

As a non-db-workaround, you can use

List<MyEntry> l = realm.where(MyEntry.class)
    .findAllSorted("name");
int pos = l.indexOf(entryWithName);
MyEntry next = l.get((pos+1)%l.size());

This does the searching outside of the db. Possibly not as well-performing, and not as readable, but it should work.

like image 104
serv-inc Avatar answered Sep 17 '26 12:09

serv-inc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!