如果我敢与迄今为止发布的所有答案相反,那么对您的文字问题的最简单答案是在您的自定义类上实现一个 getter,该类返回调整后的字符串:
- (NSString *)canonicalName
{
if([self.name rangeOfString:@"The "].location == 0)
return [self.name subStringFromIndex:4];
// etc, etc
}
然后通过将“canonicalName”指定给排序描述符进行排序。
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 via localizedCompare:
, which has been available on iOS since the beginning. You're otherwise pretty much reinventing a non-trivial wheel.
E.g.
[array sortUsingComparator:
^NSComparisonResult(YourObjectType *obj1, YourObjectType *obj2)
{
return [obj1.name localizedCompare:obj2.name];
}];