如何对包含 NSDictionary 的 NSArray 进行排序?
Posted
技术标签:
【中文标题】如何对包含 NSDictionary 的 NSArray 进行排序?【英文标题】:How to sort a NSArray which contains NSDictionary? 【发布时间】:2011-01-22 10:03:25 【问题描述】:我有一个这样的列表:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>highscores</key>
<array>
<dict>
<key>highscoreInSeconds</key>
<string>9</string>
<key>levelName</key>
<string>1</string>
<key>name</key>
<string>Black</string>
</dict>
<dict>
<key>highscoreInSeconds</key>
<string>12</string>
<key>levelName</key>
<string>1</string>
<key>name</key>
<string>Black</string>
</dict>
</array>
</dict>
</plist>
现在我想按highscoreInSeconds
对其进行排序。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistDirectory = [paths objectAtIndex:0];
NSString* fullPath = [plistDirectory stringByAppendingPathComponent:@"data.plist"];
NSMutableDictionary* pData = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];
NSMutableArray* highscores = [pData valueForKey:@"highscores"];
我现在该如何排序?
非常感谢! :)
祝你有美好的一天。
PS:最后应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>highscores</key>
<array>
<dict>
<key>highscoreInSeconds</key>
<string>12</string>
<key>levelName</key>
<string>1</string>
<key>name</key>
<string>Black</string>
</dict>
<dict>
<key>highscoreInSeconds</key>
<string>9</string>
<key>levelName</key>
<string>1</string>
<key>name</key>
<string>Black</string>
</dict>
</array>
</dict>
</plist>
你不需要告诉我如何写一个我知道的 plist。
【问题讨论】:
【参考方案1】:我用这个:
NSArray* sorted = [highscoreAll sortedArrayUsingComparator:(NSComparator)^(NSDictionary *item1, NSDictionary *item2)
NSString *score1 = [item1 objectForKey:@"highscoreInSeconds"];
NSString *score2 = [item2 objectForKey:@"highscoreInSeconds"];
return [score1 compare:score2 options:NSNumericSearch];
];
它成功了。
【讨论】:
【参考方案2】:你可以使用
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context
实现一个函数,说明 2 个字典中哪一个的排序值更大。
NSInteger intSort(id value1, id value2, void *context)
id v1 = [value1 valueforkey:@""];//give required key
id v2 = [value2 valueforkey:@""];//give required key
if (v1 < v2) // do comparison based on the data type
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
【讨论】:
但我会投票赞成你的答案,因为我认为它也会起作用【参考方案3】: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistDirectory = [paths objectAtIndex:0];
NSString* fullPath = [plistDirectory stringByAppendingPathComponent:@"data.plist"];
NSMutableDictionary* pData = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];
NSMutableArray* highscores = [pData valueForKey:@"highscores"];
现在使用...
您可以使用 NSSortDescriptor
进行此操作,并且非常易于使用。
只需提供您要对数组进行排序的键..
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"highscores" ascending:YES];
[highscores sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
[descriptor release];
希望对你有帮助,
【讨论】:
【参考方案4】:NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest" ascending:YES];
[stories sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
recent = [stories copy];
【讨论】:
键值是int,现在怎么排序呢?以上是关于如何对包含 NSDictionary 的 NSArray 进行排序?的主要内容,如果未能解决你的问题,请参考以下文章
使用 NSPredicate 根据 NSDictionary 键过滤 NSArray
使用包含 NSDictionary 的 NSMutableArray 进行快速枚举
如何序列化包含 NSNull 的 NSDictionary/NSArray/Property List (plist)
如何在 Alamofire 中保存对 NSDictionary 的响应值?
带有 NSArray 的 UITableview 包含 NSDictionary 和 NSDictionary 包含另一个 NSDictionary