Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a NSArray which contains NSDictionary?

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.

like image 746
cocos2dbeginner Avatar asked Jan 22 '11 10:01

cocos2dbeginner


3 Answers

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.

like image 89
cocos2dbeginner Avatar answered Sep 23 '22 20:09

cocos2dbeginner


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;
}
like image 44
Girish Kolari Avatar answered Sep 25 '22 20:09

Girish Kolari


 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,

like image 20
Ajay Sharma Avatar answered Sep 22 '22 20:09

Ajay Sharma