简体   繁体   English

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

[英]How to sort a NSArray which contains NSDictionary?

I have a plist like this: 我有一个像这样的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>

Now I want to sort this by the highscoreInSeconds . 现在我想通过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: 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>

You don't need to say me how to write a plist i knew that. 你不需要说我怎么写一个我知道的plist。

I use this: 我用这个:

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

and it worked. 它起作用了。

You can use 您可以使用

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context

Implement a function which say which of the 2 dictionary bigger in sorting value. 实现一个函数,说明在排序值中哪两个字典更大。

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

Now use... You can user NSSortDescriptor for this & very easy to use. 现在使用...您可以NSSortDescriptor使用NSSortDescriptor并且非常容易使用。 just provide the key by which you want to sort the array.. 只提供您想要对数组进行排序的键。

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"highscores" ascending:YES];
[highscores sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
[descriptor release];

Hope will for you, 希望对你而言,

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest"      ascending:YES];
[stories sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
recent = [stories copy];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM