title: How to sort Core Data results based on an attribute of related object collection?
body: <p>Setup: I have a collection of parent objects, call them ObjectA. Each ObjectA has a one-to-many relation to ObjectB. So, one ObjectA may contain 0..n ObjectB-s, and each ObjectB has a specific ObjectA as its parent.</p>
  
  <p>Now, I would like to do a Core Data fetch of ObjectA-s, where they are sorted by their latest ObjectB. Is it possible to create a sort descriptor for that?</p>
  
  <p>There is <a href="https://stackoverflow.com/questions/5768123/core-data-sort-by-relationships-attribute">a related question</a> that describes exactly the same situation. The answer suggests denormalizing the attribute from ObjectB into ObjectA. This would be OK if there really is no way to do this with one fetch request.</p>
  
  <p>The related question also mentions:</p>
  
  <blockquote>
    <p>Actually, I just had an idea! Maybe I can sort Conversations by messages.@max.sortedDate…</p>
  </blockquote>
  
  <p>I tried. It doesn’t seem to be possible. I get this error:</p>
  
  <pre><code>2012-10-05 17:51:42.813 xxx[6398:c07] *** Terminating app due to uncaught
  exception 'NSInvalidArgumentException', reason: 'Keypath containing
  KVC aggregate where there shouldn't be one; failed to handle
  ObjectB.@max.creationTime'
  </code></pre>
  
  <p>Is denormalizing the attribute into ObjectA the only/best solution?</p>