question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to sort `RealmResults` by two columns?

See original GitHub issue

I 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:closed
  • Created 9 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
benjaminprakash88commented, Dec 8, 2014

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

0reactions
knethcommented, Jan 23, 2015

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found