ios - Efficent way to sort 'Person.firstName' under first alphabetically letter -
i iterate through long list of names, assign each 'person' object. person
object has firstname
, lastname
. looking efficent way sort firstnames
beginning letter a
through z
. have come solutions not efficent, or there surely easier way creating '24' arrays each letter of alphabet.
for(int = 0; < numberofpeople; i++) { ... if ([person.firstname hasprefix:@"a"] || [person.firstname hasprefix:@"a"]) { [self.lettera_array addobject:person];//nsmutablearray } ... }
i similar below example if work objects, unsure:
nsdictionary *animals = @{@"b" : @[@"bear", @"black swan", @"buffalo"], @"c" : @[@"camel", @"cockatoo"], @"d" : @[@"dog", @"donkey"], @"e" : @[@"emu"], @"g" : @[@"giraffe", @"greater rhea"], @"h" : @[@"hippopotamus", @"horse"], @"k" : @[@"koala"], @"l" : @[@"lion", @"llama"], @"m" : @[@"manatus", @"meerkat"], @"p" : @[@"panda", @"peacock", @"pig", @"platypus", @"polar bear"], @"r" : @[@"rhinoceros"], @"s" : @[@"seagull"], @"t" : @[@"tasmania devil"], @"w" : @[@"whale", @"whale shark", @"wombat"]};
you this:
nsarray *names = @[@"bob", @"book2", @"august", @"zulu", @"agnoz"]; nsmutabledictionary *buckets = [[nsmutabledictionary alloc] init]; (nsstring *name in names) { nsstring *index = [name substringtoindex:1]; if (!buckets[index]) { buckets[index] = [[nsmutablearray alloc] init]; } [buckets[index] addobject:name]; }
Comments
Post a Comment