I have an NSArray of objects (not NSStrings, but custom objects). Each object has a value obj.name I know how to sort the NSMutableArray by name as shown below
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[array sortUsingDescriptors:[NSArray arrayWithObject:sort]];
However, some of the names start with “The”, “A”, or “An” all of which I want to skip over to the next word in the name if they appear. Is there anyway to avoid those three cases and automatically shift to the next word if they appear?
If I dare be contrary to all the answers posted to date, the easiest answer to your literal question would be to implement a getter on your custom class that returned the adjusted string:
Then sort by specifying ‘canonicalName’ to the sort descriptor.
However, you should first consider following the advice in QA1159 and just using
NSString‘s inherent ability to order itself as per the same rules applied by the Finder, which I think supersets the rules you’re trying to implement. So you want to end up comparing strings vialocalizedCompare:, which has been available on iOS since the beginning. You’re otherwise pretty much reinventing a non-trivial wheel.E.g.