汉字转字母分组

+ (void)AutoShelvesSortFromData:(NSArray *)sourceArr result:(void(^)(NSDictionary *result))resultBlock{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       __block NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [sourceArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self sortWithModel:obj resultDict:dict];
        }];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            !resultBlock?:resultBlock(dict);
        });
    });
}

+ (void)sortWithModel:(id)model resultDict:(NSMutableDictionary *)resultDict{
    NSString *string = model.name;
    
    //去除空格
    string = [string trim];
    if ([string length]) {
        NSMutableString *mutableStr = [[NSMutableString alloc]initWithString:string];
        
        if (CFStringTransform((__bridge CFMutableStringRef)mutableStr,0,kCFStringTransformMandarinLatin,NO)) {
            
        }
        //获取文本拼音
        if (CFStringTransform((__bridge CFMutableStringRef)mutableStr,0,kCFStringTransformStripDiacritics,NO)) {
            NSString *str = [NSString stringWithString:mutableStr];
            //转成大写
            str = [str uppercaseString];
            //获取首字母
            NSString *key = [str substringToIndex:1];
            //根据首字母获取该分组数据
            NSMutableArray *subArray = [resultDict objectForKey:key];
            //不存在该分组,则创建
            if (!subArray) {
                subArray = [NSMutableArray array];
                //存储该分组
                [resultDict setObject:subArray forKey:key];
            }
            model.firstChar = key;
            [subArray addObject:model];
        }
    }
}

你可能感兴趣的:(汉字转字母分组)