实现NSDictionary的排序

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"1",
                                @"2",@"abc",
                                @"3",@"abk",
                                @"4",@"key", nil];
    [dict setValue:@"5" forKey:@"key5"];
    
//    NSLog(@"%@",dict);
    
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
//        NSLog(@"%@",obj);
    }];
    
    NSUInteger count = dict.count;
    NSLog(@"%lu",(unsigned long)count);

   NSArray *array3 = [[dict allKeys]
                            sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    NSMutableDictionary *dictNew = [[NSMutableDictionary alloc]init];
    [array3 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        NSLog(@"idx %lu",(unsigned long)idx);
//        NSLog(@"obj %@",obj);
        NSString* ind = [NSString stringWithFormat:@"%lu",(unsigned long)idx];
        
        [dictNew setObject:obj forKey:ind];
       
        
    }];
     NSLog(@"%@",dictNew);
    
}

你可能感兴趣的:(实现NSDictionary的排序)