Realm-cocoa: Support sorting by keypath

Created on 30 Dec 2014  路  61Comments  路  Source: realm/realm-cocoa

Lets say I have:

 class Quote: RLMObject {
   dynamic var sentence = ""
 }

Could I sort an RLMResults, generated by a query, by the length of the sentence property?

T-Enhancement

Most helpful comment

Any updates on this? This seems to keep popping up in our domain (ie: for a simple Chat and Messages, want to sort chats by last message's CreatedAt date). Right now have to duplicate the data ("LastMessageCreatedAt" property on chat to do this.) These must be explicitly updated every time a new message appears in order for both sorting AND notifications to work properly (ie: Chats list screen to update to display the latest chat at the top).

Would be tremendously better if this was supported natively.

All 61 comments

To do this with the current version of Realm, you'd have to also store a sentenceLength in your model.

Hopefully #1265 leads to a better solution, but that's still very early in the evaluation process.

I think the proper solution here is to support sorting by keypath. Will change the issue name accordingly.

I recently ran into a similar issue.

Would this also include being able to sort based on a property on a related object? For example, if I have two models with the relationship Car -> Color, I would like to be able to sort the Cars by color.name.

That's precisely what this issue is tracking.

I'm very interested in this as well.

+1

+1 I expected this to work same way subqueries do!

+1

+1

+1 (same way as with subqueries plz)

+1

Any updates on this? This seems to keep popping up in our domain (ie: for a simple Chat and Messages, want to sort chats by last message's CreatedAt date). Right now have to duplicate the data ("LastMessageCreatedAt" property on chat to do this.) These must be explicitly updated every time a new message appears in order for both sorting AND notifications to work properly (ie: Chats list screen to update to display the latest chat at the top).

Would be tremendously better if this was supported natively.

Sorry, nothing to report on this feature.

+1 (same way as with subqueries person.dog.name)

+1

+1
I already asked a question on this issue on Stackoverflow before I was aware of this issue tracker at Github. Hopefully, this functionality could be added to Realm in the near future.
http://stackoverflow.com/questions/37316378/how-to-combine-swift-sort-methods-with-realm-containers

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

Any plans on this now that 1.1 is out?

I'm sure the Swift 3 migration has kept the team very busy but it would be great to see some new features come in not just the platform support =)

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

People, use reactions instead of writing "+1". It doesn't help anyone. Just getting too much useless notifications.

Support for sorting based on simple key paths (traversing to-one relationships only, with no support for collection operators or non-persisted properties) is being added in #4505.

This is a really great improvement!

I was just wondering whether key path sorting for to-many relationships will be supported in a future release as well? For my use case it would be even better to sort like:
dogOwners.sorted(byKeyPath: "dogs.last.age")

Sorting over to-many relationships is a bit more involved than sorting over to-one relationships. Would you mind filing a new issue so we can track demand for that functionality?

I just filed a new issue in #4554 for this.

Was this page helpful?
0 / 5 - 0 ratings