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

Add ability to sort RLMResults using a comparison block

Open jpsim opened this issue 10 years ago • 38 comments

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?

jpsim avatar Dec 23 '14 22:12 jpsim

I don't think this is something we will want to do. First it requires creating accessors for every element which can get kind of slow. Second, we will have to hold onto the sort block as long as the RLMResults exists and to reapply that block for live queries. This obviously has potential problems.

I think what we should do instead is to bring back the arrayForRange: pr and allow users to sort the NSArray using a block. The key is that users understand they are creating an array of accessors, and providing such a method where they do this explicitly is probably the best way to accomplish this.

alazier avatar Dec 24 '14 16:12 alazier

I agree there are disadvantages to this approach, but it's still better than the alternatives. Unless there's another solution that we have yet to consider.

I can think of a few downsides to encouraging users to use an -[RLMResults arrayWithRange:] method for custom sorting:

  1. It's error-prone. If a user forgets to recalculate their sorted array when data changes, not only will they be accessing stale data, but they'll likely also be accessing invalid objects.
  2. It's a different interface. If users are already displaying an RLMResults and they want to tweak how it's sorted, they'll have to refactor the code accessing the data. If (when) we create a suite of UIKit extensions to back controls by Realm, we'd need to support both interfaces.
  3. It's complicating a common use case. Custom sorting is a common enough feature request that it's worth us doing once rather than many of our users independently.
  4. A user's solution is likely to be slower than ours. This is pretty safe to assume. If someone comes up with a faster way to do it, they'll hopefully share their approach with us so we can improve ours, to everyone's benefit.

This being said, there might be another approach we have yet to consider, but until then, I think an -[RLMResults sortedResultsUsingBlock:] is our best bet.

jpsim avatar Dec 30 '14 01:12 jpsim

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.

ceeK avatar Feb 09 '15 11:02 ceeK

I guess we should add this and make clear in the docs that performance will be much worse than using sortDescriptors.

This is still blocked on core support support for manual construction and freezing of TableViews

alazier avatar Feb 09 '15 16:02 alazier

+1

I'm dealing with the same use case that @ceeK highlighted, sorting on a computed distance property.

michaelcameron avatar Feb 22 '15 03:02 michaelcameron

+1, i also need this feature.

jchesne avatar Mar 20 '15 15:03 jchesne

+1 for needing it in particular with distance

devpascoe avatar Apr 30 '15 01:04 devpascoe

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!).

leegoodrich avatar May 19 '15 03:05 leegoodrich

+1

bcapps avatar Aug 06 '15 19:08 bcapps

+1

beloso avatar Aug 26 '15 17:08 beloso

+1

dltlr avatar Jan 12 '16 18:01 dltlr

+1

rex-remind101 avatar Jan 19 '16 19:01 rex-remind101

+1

FelixLisczyk avatar Jan 23 '16 09:01 FelixLisczyk

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.

rex-remind101 avatar Jan 25 '16 18:01 rex-remind101

+1

cianiandreadev avatar May 09 '16 11:05 cianiandreadev

+1

hovox avatar Jul 13 '16 10:07 hovox

+1

jimqin-alivecor avatar Sep 08 '16 22:09 jimqin-alivecor

+1 here as well. Trying to sort based on one optional NSDate and another non-optional NSDate. Trying to look at whether the optional date exists and if so us it for sorting, if not fall back to the non-optional date.

LunaCodeGirl avatar Sep 18 '16 20:09 LunaCodeGirl

+1

o15a3d4l11s2 avatar Oct 01 '16 12:10 o15a3d4l11s2

+1

peterpaulis avatar Nov 10 '16 12:11 peterpaulis

you should also annotate that sortedResultsUsingDescriptors expects an array of RLMSortDescriptor not a NSSortDescriptor...

peterpaulis avatar Nov 10 '16 12:11 peterpaulis

@peterpaulis nice catch! See #4318.

jpsim avatar Nov 10 '16 17:11 jpsim

+1

kenneth488 avatar May 08 '17 10:05 kenneth488

+1

weibel avatar May 11 '17 08:05 weibel

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

joeFlypay avatar May 19 '17 14:05 joeFlypay

+1

oparinov avatar Aug 28 '17 14:08 oparinov

+1

punithbm avatar Apr 23 '18 07:04 punithbm

+1

juliensagot avatar Jun 24 '18 00:06 juliensagot

+1

aaronrogers avatar Jul 02 '18 22:07 aaronrogers

Was this implemented already or no?

trant avatar Nov 02 '18 16:11 trant