Is there a way to sort `RealmResults` by two columns?
See original GitHub issueI am trying to sort RealmResults
by two columns:
RealmResults<User> results = realm.where(User.class).equalTo("relevant", true).findAll();
results.sort("balance", RealmResults.SORT_ORDER_ASCENDING);
results.sort("visible", RealmResults.SORT_ORDER_ASCENDING);
setListViewData(getActivity(), results, true);
But in the listview the results are only sorted by the column visible
.
So is there a way to sort by two columns?
I am using version 0.75.1.
Issue Analytics
- State:
- Created 9 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
android - Realm java sort with multiple fields - Stack Overflow
sort ("venueTitle", RealmResults.SORT_ORDER_ASCENDING);. How can I sort by multiple properties? Adding another sort line just resets the order of ...
Read more >io.realm.RealmResults.sort java code examples - Tabnine
Sorts existing {@link io.realm.RealmResults} using two fields. * * @param fieldName1 first field name. * @param sortOrder1 sort order for first field.
Read more >RealmQuery (Realm 4.3.2) - MongoDB
A RealmQuery encapsulates a query on a Realm or a RealmResults using the Builder pattern. The query is executed using either findAll() or...
Read more >RealmQuery (Realm 0.87.5)
A RealmQuery encapsulates a query on a Realm or a RealmResults using the Builder pattern. The query is executed using either findAll() or...
Read more >How to Sort By Multiple Fields (Columns) in Microsoft Access ...
In this Microsoft Access tutorial, I'm going to teach you how to sort by multiple fields (they're fields in Access, folks, not columns...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hello @zsavely. Until proper support for multi field sorting is added to Realm, there are a few workarounds. The most generic is to make a computed field containing both values (maybe as a string in your case), and then sort by that. That obviously has the drawback of using more space, but only you can know if that’s ok.
In your particular case it looks like one of your fields “visible” might be a boolean, and then you could sort in two steps:
RealmResults<User> results = realm.where(User.class).equalTo(“relevant”, true).findAll(); RealmResults<User> results2 = results.where().equalTo(“visible”, boolean).findAll(); results2.sort(“balance”, RealmResults.SORT_ORDER_ASCENDING);
Hope this helps you until we support multilevel sorting.
Cheers
@njzk2 Not really. The initial report was using single field sorting twice which is sorting two different lists. After that report, we have implemented multi-field sorting.