typedefNS_OPTIONS(NSUInteger, NSStringCompareOptions) {
NSCaseInsensitiveSearch = 1, //不区分大小写比较
NSLiteralSearch = 2, //逐字节比较 区分大小写
NSBackwardsSearch = 4, //从字符串末尾开始搜索
NSAnchoredSearch = 8, //搜索限制范围的字符串
NSNumericSearch = 64, //按照字符串里的数字为依据,算出顺序。例如 Foo2.txt < Foo7.txt < Foo25.txt
NSDiacriticInsensitiveSearchNS_ENUM_AVAILABLE(10_5, 2_0) = 128,//忽略 "-" 符号的比较
NSWidthInsensitiveSearchNS_ENUM_AVAILABLE(10_5, 2_0) = 256,//忽略字符串的长度,比较出结果
NSForcedOrderingSearchNS_ENUM_AVAILABLE(10_5, 2_0) = 512,//忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending
NSRegularExpressionSearchNS_ENUM_AVAILABLE(10_7, 3_2) = 1024 //只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
};
NSArray *stringsArray = [NSArrayarrayWithObjects:
@"string 10",
@"String 1",
@"string 120",
@"String 1100",
@"String 02000",nil];
NSStringCompareOptions comparisonOptions =NSCaseInsensitiveSearch |NSNumericSearch|NSWidthInsensitiveSearch |NSForcedOrderingSearch;
NSLocale *currentLocale = [NSLocalecurrentLocale];
NSComparator finderSortBlock = ^(id string1,id string2) {
NSRange string1Range =NSMakeRange(0, [string1length]);
return[string1compare:string2options:comparisonOptionsrange:string1Rangelocale:currentLocale];
};
NSArray *finderSortArray = [stringsArraysortedArrayUsingComparator:finderSortBlock];
NSLog(@"finderSortArray: %@", finderSortArray);
finderSortArray: (
"String 1",
"string 10",
"string 120",
"String 1100",
"String 02000"
)