按 2 级深的键值关系对 NSArray 进行排序

Posted

技术标签:

【中文标题】按 2 级深的键值关系对 NSArray 进行排序【英文标题】:Sorting an NSArray by a key-value relationship that is 2 levels deep 【发布时间】:2010-07-02 23:00:41 【问题描述】:

我有一个 UILocalNotification 对象的 NSArray,我需要根据 UILocalNotification 的 userInfo 属性中的键进行排序,该属性是一个 NSDictionary。我知道如何按一个深度的值对 NSArrays 进行排序,例如,NSDictionaries 数组中的一个键,但是我迷失了两个深度。我想做这样的事情是伪代码:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"userInfo.personName" ascending:YES]; 
//the personName key within the userInfo NSDictionary property of a given UILocalNotification
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:&sortDescriptor count:1] autorelease];
NSMutableArray *sortedArray = [[[NSMutableArray alloc] initWithArray:[[UIApplication sharedApplication] scheduledLocalNotifications]] autorelease];
[sortedArray sortUsingDescriptors:sortDescriptors];

【问题讨论】:

【参考方案1】:

您尝试过您的代码吗? Apple 的文档 (here) 说它接受密钥路径,而不仅仅是密钥。

【讨论】:

我以为我从某个地方记起了点语法!

以上是关于按 2 级深的键值关系对 NSArray 进行排序的主要内容,如果未能解决你的问题,请参考以下文章

PHP从多个json文件中的键值排序结果

python怎么对字典进行排序

如何按数组计数对嵌套 NSArray 的 NSArray 进行排序?

对一个给定的二维数组按照指定的键值进行排序

按多个键值对数据进行排序

今天按 NSDate 对 NSArray 进行排序