typedefNS_OPTIONS(NSUInteger, NSStringCompareOptions) { NSCaseInsensitiveSearch = 1, //不區分大小寫比較 NSLiteralSearch = 2, //逐位元組比較 區分大小寫 NSBackwardsSearch = ...
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"
)