tags:

views:

11

answers:

1

I have a dictionary plist. That dictionary has A-Z arrays which have names in them. I would like to alphabetize the names in each of the arrays and save that order back to the plist. Is the best way to enumerate through the keys and do a sort on each of the arrays?

A: 
            // Full dictionary from plist
            NSString *path = [[NSBundle mainBundle] pathForResource:@"Names" ofType:@"plist"];
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
            self.mainDict = dict;
            [dict release];

            // keysArray is full of A-Z
            NSMutableArray *keyArray = [[NSMutableArray alloc] init];
            NSArray *sortedArray = [[mainDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
            [keyArray addObjectsFromArray:sortedArray];
            self.keysArray = keyArray;
            [keyArray release];

            // Sorts each array of names
            int i = 0;
            for (id keys in keysArray) {
                            NSArray *unsortedArray = [mainDict valueForKey:[keyArray objectAtIndex:i]];
                            NSArray *sorted = [unsortedArray sortedArrayUsingSelector:@selector(compare:)];
                            [mainDict setValue:sorted forKey:[keyArray objectAtIndex:i]];
                            i++;
            }

Looks like this does it. mainDict is an NSMutableDictionary. keysArray is an NSArray. Now the problem is saving it.

tazboy