Core Data (SQLite backed) custom sorting of strings
core-data, ipad, iphone
Solution
Add a new BOOL attribute called `parenthesized`, then...
- (void)setName:(NSString *)name
{
[self willChangeValueForKey:@"name"];
[self setPrimitiveName:name];
[self didChangeValueForKey:@"name"];
self.parenthesized = ([name hasPrefix:@"("] &&
[name hasSuffix:@")"]);
}
And when you do the fetch:
[fetchRequest setSortDescriptors:
[NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@"parenthesized" ascending:NO],
[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES],
nil]];
Problem
I currently have a SQLite backed Core Data app where I am fetching records sorted on firstName and then lastName. The issue is that I have some names like `(John Doe)` and they are coming back at the top of the list, it seems the default sorting behavior is done in ASCII sort order. Is there anyway to force these records to show up after `Z`? I have read about custom sort descriptors but they do not seem to work because Core Data falls back on SQLite for sorting. Update Here is a sample list of what my data looks like now ``` (Abe Simpson) (Bob Dole) (Chris Rock) Alan West Brian Regan ``` What I want it to look like ``` Alan West Brian Regan (Abe Simpson) (Bob Dole) (Chris Rock) ```