// Takes the array in question and creates an index for use in the tableview
- (NSArray *)makeSectionsIndex:(NSArray *)arrayOfDictionaries {
// Creates a mutable set to read each letter only once
NSMutableSet *sectionsMutableSet = [NSMutableSet setWithCapacity:26];
// Reads each item's country & loads it's first letter into sections set
for (int i=0; i<=[arrayOfDictionaries count]-1; i++) {
NSDictionary *aDictionary = [arrayOfDictionaries objectAtIndex:i];
NSString *aCountry = [aDictionary objectForKey:@"Country"];
NSString *aLetter = [aCountry substringToIndex:1U]; //uses first letter of string
[sectionsMutableSet addObject:aLetter];
}
// Copies the mutable set into a set & then make a mutable array of the set
NSSet *sectionsSet = [NSSet setWithSet:sectionsMutableSet];
NSMutableArray *sectionsMutableArray = [[sectionsSet allObjects] mutableCopy];
//Now let's sort the array and make it inmutable
NSMutableArray *sortedMutableArray = [[NSMutableArray alloc] init];
sortedMutableArray = [sectionsMutableArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortedIndexArray = [NSArray arrayWithArray:sortedMutableArray];
// NSLog(@"%@", sortedIndexArray);
return sortedIndexArray;
}
posted from MyMapView, masterViewController by CPL