Try the search, it's linked to some great forums

Thursday, August 9, 2012

Creating and array of index letters

The following method allows you to supply an array of dictionaries and obtain a sorted index array of any key for use in an indexed tableView;
// 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