Q:

How can NSArrayController sort on a to-many relationship?

Using CoreData, I have an entity "Bookmark", that has an to-many relationship named 'tags' to another entity "Tag", and some commun attributes (string, date, ...).
In a NSTableView we display the Bookmarks entity via Binding:

the NSArrayController is binded to File's Owner.managedObjectContext
(standard XCode CoreData template, the managedObjectContext is in the AppDelegate)
The columns in TableView are binded to their respective attribute. In particular the Tag column is binded to this arrayController.arrangedObjects.tags with a subclass of NSValueTransformer so that we can show, as an NSString, a summary of the to-many relationship.

It work. Now when I click on column header the whole table view get sorted correctly except for the 'tag' column where I get this:
-[_NSFaultingMutableSet compare:]: unrecognized selector sent to instance

For sure the "Set" from this to-many relationship doesn't respond to the selector 'compare:'.
Question:
How can I make this work ? How can I sort on a to-many relationship ?
Are something like the ValueTransformer available ? If I could supply a custom class that would do the compare: for the ArrayController to know...

A:

One possible hack: since _NSFaultingMutableSet is a NSSet, we can add the selector 'compare:' via a categorie.
@interface NSSet (someAdditions)
   - (NSComparisonResult)compare:(NSSet *)anotherSet;
@end

@implementation NSSet (someAdditions)
   - (NSComparisonResult)compare:(NSSet *)aSet {
      ...  
   }
@end

we can now implement this compare: selector as we wish, like comparing the count of each set, or their NSString representation in some way.
It work in my App. I re-enabled the 'Creates Sort Descriptor' on the binding of the NSTableColumn and can now click on the header of my tableView to sort.
It's a hack because it affect all NSSet... But at least I have my hook.
What do you think ?

