是否有一种通过键值对plist(字典数组)进行排序的简单方法?

问题描述 投票:4回答:6

我需要通过键值对plist(字典字典)进行重新排序。

在此示例内容中,我想按键名称(Matt,Joe)的值进行排序:

<dict>
    <key>Name</key>
    <string>Matt</string>
    <key>Details</key>
    <string>Me</string>
</dict>
<dict>
    <key>Name</key>
    <string>Joe</string>
    <key>Details</key>
    <string>You</string>
</dict>

有没有简单的方法?我不想每次运行应用程序时都用代码来完成它,我只想对数据文件执行它。

有什么想法吗?

乐于使用任何工具来完成此任务:忍者参数,可在命令行上进行排序,plist编辑器,文本编辑器或其他工具。

iphone cocoa sorting plist nsdictionary
6个回答
13
投票

这是另一种编码解决方案,但是制作环绕它的基本命令行工具并不难:


6
投票

我编写了一个小的Python脚本,该脚本从标准输入读取并写入标准输出:


3
投票
// Our key array
NSMutableArray *unsortedKeys = [NSMutableArray array];
// Assume we have some array of dictionaries
for( NSDictionary *dict in dictionaryArray ) {
  NSString *key = [dict objectForKey:@"Name"];
  if( key )
    [unsortedKeys addObject:key];
}
NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(compare:)];

// Do things with the keys...

0
投票

Python是您的朋友:http://effbot.org/zone/element-sort.htm:-)


0
投票

我最终编写了五行PHP脚本来对它们重新排序。我想我仍然需要学习很多可可粉,然后才能快速,舒适地使用它。


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.