Realm-java: Sorting NULL first/last

Created on 24 Jul 2016  路  9Comments  路  Source: realm/realm-java

When sorting it would be nice to be able to specify if NULL values in the column should be at the beginning or end of the results.

The current behaviour is for NULLs to be at the beginning of the results if ASC and end if DESC.

In my application I am querying a list of records by "date due descending".
The requirement for the UI is to display records without a due date first followed by those with a due date. Currently all those without a due date appear at the bottom of the list.

To make it easy to use I would just suggest enhancing the io.realm.Sort enum by adding ASCENDING_NULLS_FIRST/LAST and DESCENDING_NULLS_FIRST/LAST.

O-Community Pipeline-Idea-Backlog T-Enhancement

Most helpful comment

You can solve the issue with two fields in your object instead of using multiple lists, which is much easier if you can update both fields simultaneously.

  • For example one for your date and one if it is available
class RealmMediaWrapper {
    val releaseDate: Long
    val hasReleaseDate: Boolean
}
  • Sort first whether the date is available and then the date itself. For example:
if(sort == Sort.ASCENDING)
   results.sort(RealmConstant.HAS_RELEASE_DATE, Sort.DESCENDING, RealmConstant.RELEASE_DATE, sort)
else
   results.sort(RealmConstant.RELEASE_DATE, sort)
  • Migrate old objects if necessary
transform {
   val value = it.getLong("releaseDate")
   it.setBoolean("hasReleaseDate", value != 0L)
}

All 9 comments

In the meantime, you can construct your displayed elements from multiple RealmResults.

http://stackoverflow.com/a/34976540/2413303

@JM-Agrimap

It saddens me to tell you the feature you've sought after is not currently supported. I'll place this suggestion in enhancement to track in the future. Thanks!

EDIT > You might want to take a look at @Zhuinden's suggestion to combine two results. i.e. The one sorted witout NULL and the one with NULL only.

Internal Note

It looks like this issue has been discussed about an year ago here https://github.com/realm/realm-java/issues/3209 but revoked due to be aligned with other databases.

It's almost 2019 :(

And you're pretty much the third person in almost 4 years to ask for it so far, so don't keep your hopes up.

Well I'll be the fourth; I'm currently in need of exactly this capability.

I'm sure the number is a lot more than four. Everybody is just hacking it with multiple RealmResults.

You can solve the issue with two fields in your object instead of using multiple lists, which is much easier if you can update both fields simultaneously.

  • For example one for your date and one if it is available
class RealmMediaWrapper {
    val releaseDate: Long
    val hasReleaseDate: Boolean
}
  • Sort first whether the date is available and then the date itself. For example:
if(sort == Sort.ASCENDING)
   results.sort(RealmConstant.HAS_RELEASE_DATE, Sort.DESCENDING, RealmConstant.RELEASE_DATE, sort)
else
   results.sort(RealmConstant.RELEASE_DATE, sort)
  • Migrate old objects if necessary
transform {
   val value = it.getLong("releaseDate")
   it.setBoolean("hasReleaseDate", value != 0L)
}

Thanks for the trick @chrisbln - just tried it out and it's working great for now. A little annoying to add an extra field for this, but sorting in realm is so much faster than trying to sort in memory.

Was this page helpful?
0 / 5 - 0 ratings