views:

77

answers:

1

I've got a simple Core Data application that I'm working on to display my movie collection. I'm using an NSTableView, with it's columns bound to attributes of my Core Data store through an NSArrayController object. At this point the columns sort fine(for numeric values) when the column headers are clicked on.

The issue I'm having is with the String sorting, they sort, however it's done in standard string fashion, with Uppercase letters preceding lowercase(i.e. Z before a). In addition to getting the case sorting to work properly, I would like to be able to ignore a prefix of "The " or "A " when sorting the strings.

What is the best way to go about this in Objective-C/Cocoa?

+6  A: 

In Interface Builder, you set the Sort Key field of each table column to correspond to which sortDescriptor you want applied to your managed objects.

For each column that you want to ignore "A" and "the", you'll have to create an extra method on your managed objects. If the property you're sorting is title, then create a new method called cleanedTitle that basically takes [self title] and strips out "a" and "the". Then in Interface Builder, tell your table column to use the cleanedTitle sort key, instead of the title sort key.

Dave DeLong
I like this idea, very clever. I'm still pretty new to objc and cocoa. Would I be wanting to override the -(void) setValue:(NSString *) forKey:(NSString *) method of NSMangedObject? If so, how would I do this? Do I just write a new class extending that, and use it as the class for my Core Data entity? Thanks.
David Barry
David Barry: You would write a subclass of NSManagedObject, and use it as the class of your Core Data entity. (So, yes to the “extend” part.) Don't override `setValue:forKey:`; add a new method named `cleanedTitle` that derives its value from `[self title]`. You'll also want to make that property dependent on the model property: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html#//apple_ref/occ/clm/NSObject/keyPathsForValuesAffectingValueForKey:
Peter Hosey
Thanks peter, that sounds like a much cleaner solution than overriding setValue: forKey:.
David Barry