realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Support custom sorting in RealmResult and RealmList

Open snowpong opened this issue 10 years ago • 3 comments

Goal

I want to be able to sort RealmResult or RealmList in a custom way. For example it is impossible to sort in Realm for Android by Location. I know there is a specific issue tracking this, but thinking more generally - there will always be another type or way of sorting you haven't gotten around to implementing.

Expected Results

I can implement my own https://developer.android.com/reference/java/util/Comparator.html or similar, and use it directly in a Realm query.

Actual Results

I have to copy the result to a local list and run my custom sort there, loosing the benefits of change listeners etc.

snowpong avatar Oct 12 '16 08:10 snowpong

https://github.com/realm/realm-java/issues/2313

https://github.com/realm/realm-java/issues/2744

Zhuinden avatar Oct 12 '16 08:10 Zhuinden

Hi @snowpong . Thanks for the suggestion. As @Zhuinden points out this is already being tracked by especially #2313. The biggest problem with a custom sorting Comparator is that the sorting is done in C++ while the Comparator is java code. Calling back into Java to do sorting would probably be extremely expensive. That said we haven't really measured it yet, so it might be acceptable for small results.

In any case, i'll close this as an duplicate and let the discussion continue in #2313

cmelchior avatar Oct 12 '16 08:10 cmelchior

@cmelchior Fair enough. I should have searched better when posting the feature request. I see the issue with crossing the JNI barrier and speed.

snowpong avatar Oct 12 '16 09:10 snowpong