0

I've been prowling through the SO search results and can't (thus far) find an answer to this question.

Given a data entity A that has a one-to-many relationship to entity B, is there any way to have my NSFetchedResult sort on a property of entity B. i.e. Can I have my sort descriptor key drill down into entity B? I've tried dot notation, etc, but nothing is working.

e.g. Entity A has a relationship inEntityB. Entity B has a property entityBproperty. How can I sort a list of Entity A's based on the entityBproperty value of their respective inEntityB relationships?

Something like (and no this doesn't work):

request.sortDescriptors = [NSArray arrayWithObjects:
    [NSSortDescriptor sortDescriptorWithKey:@"entityAproperty" ascending:NO selector:@selector(localizedStandardCompare:)], 
    [NSSortDescriptor sortDescriptorWithKey:@"inEntityB.entityBproperty" ascending:NO selector:@selector(localizedStandardCompare:)], 
    nil];

It seems awfully inefficient to duplicate, in Entity A, the entityBproperty that I want to sort by. Isn't the whole point of relationships that you can compartmentalize your data model to keep it more manageable?

Any advice much appreciated! Thanks!

Edit: I'm not really sure what additional information I can add to help clarify the question, but given the lack of response thus far, if anyone has suggestions for what else I might add here, I'm all ears. Unfortunately I don't have bounty privileges yet, or I'd consider one in this case, as this seems like a potentially more obscure question.

J Sreedhar
  • 52
  • 8
Murdock
  • 826
  • 9
  • 21
  • did you find a solution or a work-around to this problem? I have exactly the same situation. Please let me know if you can provide anything helpful. Thank you. – KDaker Jan 28 '12 at 15:50
  • Unfortunately not. It seems like there must be a way to do it, though. You may be able to do it using dynamic attributes. – Murdock Feb 23 '12 at 02:58
  • Are we asking the same question? Here's mine: http://stackoverflow.com/questions/13002915/possible-for-an-nssortdescriptor-to-use-a-specific-object-in-a-to-many-relations – Bryan Irace Oct 22 '12 at 02:13

1 Answers1

0

Looks like a possible duplicate of this question Help sorting an NSArray across two properties (with NSSortDescriptor?)

Unfortunately it seems like there's no easy way to do this with the dot syntax like you have above.

Community
  • 1
  • 1
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
  • The question you've referenced does not seem to be concerned with Core Data. My question, however, is a core data `NSFetchRequest` question at its core (pardon the pun). – Murdock Oct 11 '11 at 21:29