5

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.

4 Answers 4

8

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.

4

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;
}
1
  • but i will vote your answer up because i think it will work too Jan 22, 2011 at 10:23
4
 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. 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,

0
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest"      ascending:YES];
[stories sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
recent = [stories copy];
1
  • key value is int so how can sort it now? Nov 1, 2017 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.