實現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);
    
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容