Can i sort the NSDictionary on basis of key in Objective-C?

后端 未结 2 809
独厮守ぢ
独厮守ぢ 2020-11-27 16:17

Can I sort the NSDictionary on basis of key?

2条回答
  •  隐瞒了意图╮
    2020-11-27 16:24

    It is much simpler to use objectsForKeys:notFoundMarker: for that

    NSDictionary *yourDictionary;
    NSArray *sortedKeys = [yourDictionary.allKeys sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES]]];
    // As we using same keys from dictionary, we can put any non-nil value for the notFoundMarker, because it will never used
    NSArray *sortedValues = [yourDictionary objectsForKeys:sortedKeys notFoundMarker:@""];
    

提交回复
热议问题