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.
In the meantime, you can construct your displayed elements from multiple RealmResults.
@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.
class RealmMediaWrapper {
val releaseDate: Long
val hasReleaseDate: Boolean
}
if(sort == Sort.ASCENDING)
results.sort(RealmConstant.HAS_RELEASE_DATE, Sort.DESCENDING, RealmConstant.RELEASE_DATE, sort)
else
results.sort(RealmConstant.RELEASE_DATE, sort)
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.
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.