如何对包含 NSDictionary 的 NSArray 进行排序?
我有一个像这样的 plist:
<?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。
I have a plist like this:
<?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>
Now I want to sort this by the 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"];
How can I sort this now?
Thank you very much!
:)
Have a nice day.
PS: It should look like this at the end:
<?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>
You don't need to say me how to write a plist i knew that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我用这个:
它有效。
I use this:
and it worked.
您可以使用
实现一个函数来说明两个字典中哪一个的排序值更大。
You can use
Implement a function which say which of the 2 dictionary bigger in sorting value.
现在用...
您可以为此使用
NSSortDescriptor
非常容易使用。只需提供您想要对数组进行排序的键即可。
希望对您有用,
Now use...
You can user
NSSortDescriptor
for this & very easy to use.just provide the key by which you want to sort the array..
Hope will for you,