字典排序

2018-07-17  本文已影响0人  哈利波特会魔法

-(NSMutableArray *)stringWithDict:(NSDictionary*)dict{
    NSMutableArray *result = [NSMutableArray new];
    
    NSArray*keys = [dict allKeys];
    NSArray*sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];//正序
    }];
    
    for(NSString*categoryId in sortedArray) {
        id value = [dict objectForKey:categoryId];
        
        if([value isKindOfClass:[NSDictionary class]]) {
            
            value = [self stringWithDict:value];
        }
        [result addObject:value];
//        if([str length] !=0) {
//
//            str = [str stringByAppendingString:@","];
//        }
//        str = [str stringByAppendingFormat:@"%@:%@",categoryId,value];
    }
    NSLog(@"str: %@", result);
    return result;
}

key按照升序排序,返回一个排好序的数组。

上一篇下一篇

猜你喜欢

热点阅读