gpt4 book ai didi

iphone - 如何对包含 NSDictionary 的 NSArray 进行排序?

转载 作者:行者123 更新时间:2023-12-03 18:50:36 25 4
gpt4 key购买 nike

我有一个像这样的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。

最佳答案

我用这个:

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];
}];

并且成功了。

关于iphone - 如何对包含 NSDictionary 的 NSArray 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4767219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com