首先对 NSString 的 NSArray 按阿拉伯语字母顺序排序
Sort NSArray of NSString alphabetical Arabic first
如何按字母顺序对包含字符串的 NSArray 进行排序,但我希望阿拉伯语字符串的优先级高于英语并放在已排序数组的顶部
您可以通过使用 compare:options:range:locale 使其工作:并明确指定阿拉伯语语言环境,如下所示:
NSArray *strings=[NSArray arrayWithObjects:@"as", @"ol", @"st", nil]; // also contain
NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"ar_BH"];
NSArray *sorted=[strings sortedArrayUsingComparator:^(NSString *first, NSString *second) {
return [second compare:first
options:0
range:NSMakeRange(0, [second length])
locale:locale];
}];
for (NSString *s in sorted) { NSLog(@"%@", s); }
我也是这样做的:)
-(NSArray*)arabicFirstSortedArray:(NSArray*)arr {
static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"ar"];
NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSRange string1Range = NSMakeRange(0, [obj1 length]);
return [obj1 compare:obj2 options:comparisonOptions range:string1Range locale:locale];
}];
return sortedArray;
}
这是另一种使阿拉伯字符串位于顶部但保持排序不变的方法
-(NSArray*)arabicFirstSortedArray:(NSArray*)arr {
NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
BOOL isFirstArabic = [self isArabicString:obj1];
BOOL isSecondArabic = [self isArabicString:obj2];
if (isFirstArabic == isSecondArabic) {
return NSOrderedSame;
}
else if(isFirstArabic){
return NSOrderedAscending;
}
else{
return NSOrderedDescending;
}
}];
return sortedArray;
}
-(BOOL)isArabicString:(NSString*)str {
NSString *isoLangCode = (__bridge_transfer NSString*)CFStringTokenizerCopyBestStringLanguage((__bridge CFStringRef)str, CFRangeMake(0, str.length));
if([isoLangCode isEqualToString:@"ar"]){
return YES;
}
else{
return NO;
}
}
如何按字母顺序对包含字符串的 NSArray 进行排序,但我希望阿拉伯语字符串的优先级高于英语并放在已排序数组的顶部
您可以通过使用 compare:options:range:locale 使其工作:并明确指定阿拉伯语语言环境,如下所示:
NSArray *strings=[NSArray arrayWithObjects:@"as", @"ol", @"st", nil]; // also contain
NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"ar_BH"];
NSArray *sorted=[strings sortedArrayUsingComparator:^(NSString *first, NSString *second) {
return [second compare:first
options:0
range:NSMakeRange(0, [second length])
locale:locale];
}];
for (NSString *s in sorted) { NSLog(@"%@", s); }
我也是这样做的:)
-(NSArray*)arabicFirstSortedArray:(NSArray*)arr {
static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"ar"];
NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSRange string1Range = NSMakeRange(0, [obj1 length]);
return [obj1 compare:obj2 options:comparisonOptions range:string1Range locale:locale];
}];
return sortedArray;
}
这是另一种使阿拉伯字符串位于顶部但保持排序不变的方法
-(NSArray*)arabicFirstSortedArray:(NSArray*)arr {
NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
BOOL isFirstArabic = [self isArabicString:obj1];
BOOL isSecondArabic = [self isArabicString:obj2];
if (isFirstArabic == isSecondArabic) {
return NSOrderedSame;
}
else if(isFirstArabic){
return NSOrderedAscending;
}
else{
return NSOrderedDescending;
}
}];
return sortedArray;
}
-(BOOL)isArabicString:(NSString*)str {
NSString *isoLangCode = (__bridge_transfer NSString*)CFStringTokenizerCopyBestStringLanguage((__bridge CFStringRef)str, CFRangeMake(0, str.length));
if([isoLangCode isEqualToString:@"ar"]){
return YES;
}
else{
return NO;
}
}