realm-swift: Add ability to sort RLMResults using a comparison block

We should discuss the feasibility of this, especially around auto-updating. It may not be fully possible to do this without the ability to freeze RLMResults. If that’s the case, we should add the blocked tag to this issue.

The API could look something like -[RLMResults sortedResultsUsingBlock:], which would require constructing the accessors for each item in the RLMResults and then accessing the properties accessed in the block, but it would allow people to sort in any way they like.

Thoughts @alazier @tgoyne?

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Reactions: 17
  • Comments: 38 (4 by maintainers)

Most upvoted comments

+1 for needing it with distance.
Even if I have my own in-memory array of location id’s sorted by distance, I still can’t sort the RLMResults using that array, as the only thing I can sort on is dumb, stored values in the DB. This really is a serious limitation. Seems to me that -[RLMResults sortedResultsUsingBlock:] and/or -[RLMResults sortedResultsUsingPredicate:] are a central requirement to loads and loads of use cases.

Just +1ing this.

I’m currently trying to sort an array of locations by a computed ‘distance’ property. This ‘distance’ property represents the distance between the location and a user selected point.

I’ve been using RLMResults as my data model for my tableview, as it has sorting built in. Unfortunately, was a bit stumped and now will have to use standard arrays.

This year it will be 10 years anniversary since the ticket was created 😃 Any updates/plans for this?

Adding my +1 to this. Even if using this came with a significant performance tradeoff versus sort descriptors (when using the feature, obviously), it has to be better than my “map to an array, run a custom sort, and regenerate on every write notification” solution I’m currently stuck with.

This would also give relief to the “sort on relationship properties” crowd (#1277) while they wait for support within sort descriptors (myself included!).

More specifically if we could have the localizedStandardCompare: option such as in NSFetchedResultsController that would cover 99% of cases. I’m not sure if that would be any easier to implement on its own.

+1 for needing it in particular with distance