ios - 从多维 NSDictionary 的子字典值创建排序数组

标签 ios objective-c nsmutablearray nsarray nsdictionary

我有一个包含子 NSDictionaries 的 NSDictionary,我想知道是否可以通过子词典的“name”键的值按字母顺序对主词典进行排序。

NSDictionary *student1 = @{@"first_name" : @"Scott", @"last_name" : @"Smith"};
NSDictionary *student2 = @{@"first_name" : @"John", @"last_name" : @"Donalson"};
NSDictionary *student3 = @{@"first_name" : @"Mary", @"last_name" : @"Patricia"};
NSDictionary *student4 = @{@"first_name" : @"Zack", @"last_name" : @"Elliot"};

NSDictionary *students = @{"001": student1, "002": student2, "003": student3, "004": student4};

我希望能够创建一个数组或可变数组,它只保存键,但根据子字典中某些键的值进行排序。像这样:

姓氏数组

["002", "004", "003", "001"];

名字数组

["002", "003", "001", "004"];

最佳答案

查看 NSDictionary 方法keysSortedByValueUsingComparator:

您提供一个比较器 block ,用于处理值(您的学生词典)并返回一个键数组。

https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/doc/uid/20000140-SW21

关于ios - 从多维 NSDictionary 的子字典值创建排序数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18472947/

相关文章:

ios - Mapbox - setCenterCoordinate 正在取消选择注释

iphone - 具有委托(delegate)方法的 NSTimer

iphone - 我将如何检测 TTTAttributedLabel 中的链接?

ios - 将数据传递到两个 View Controller 之间的 NSMutablearray,IOS 4.3

ios - @lvalue NSMutableArray' 不可转换为 'SequenceType'

objective-c - 关于 NSMutableArray 的问题

ios - Crashlytics 记录错误

ios - UINavigationBar title like Music app

带有自定义 Audio Session 的 IOS 系统声音

objective-c - 'delegate' 属性可以支持多个协议(protocol)吗?