NSString
s can be compared using the NSNumericSearch compare option.
One version:
NSInteger sort(Obj* a, Obj* b, void*) {
return [[a title] compare:[b title] options:NSNumericSearch];
}
result = [array sortedArrayUsingFunction:&sort context:nil];
Or a bit more generic:
NSInteger sort(id a, id b, void* p) {
return [[a valueForKey:(NSString*)p]
compare:[b valueForKey:(NSString*)p]
options:NSNumericSearch];
}
result = [array sortedArrayUsingFunction:&sort context:@"title"]
Or using blocks:
result = [array sortedArrayUsingComparator:^(Obj* a, Obj* b) {
return [[a title] compare:[b title] options:NSNumericSearch];
}];
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…