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.
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.
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,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With