You have to sort the keys array based on the value they link to in the dictionary, then create the second array, although I feel like you don't really need the second array. One way of achieving what you want is using the sortUsingComparator:
method in NSMutableArray
, like this:
PlistManager *pm = [[PlistManager alloc] init];
NSMutableDictionary *students = [pm getStudentsDict:ClassID];
NSMutableArray *sortedKeys = [[students allKeys] mutableCopy]; // remember to manually release the copies you create
[sortedKeys sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *student1 = [students objectForKey:obj1];
NSString *student2 = [students objectForKey:obj2];
return [student1 compare:student2]; // this does a simple comparison, look at the NSString documentation for more options
}];
// at this point your sortedKeys variable contains the keys sorted by the name of the student they point to //
// if you want to create the other array you can do so like this:
NSArray *sortedStudents = [students objectsForKeys:sortedKeys notFoundMarker:[NSNull null]];
// you can also iterate through the students like so:
for (int i = 0; i < sortedKeys.count; ++i)
{
NSString *key = sortedKeys[i];
NSString *student = [students objectForKey:key];
}
// or access directly:
NSString *studentAtIndex3 = [students objectForKey:sortedKeys[3]];
// always remember to release or autorelease your copies //
[sortedkeys release];
Hope it helps.