I have two arrays here on is from another class and one i have created in the function, my end goal is to have teamRoster sorted alphabetically, this seems to work but could someone please tell me if there is a better way? Thanks.
-(IBAction)submitAndSendBack:(id)sender{
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString *fullName = [ NSString stringWithFormat:@"%@ %@",firstName.text, lastName.text];
[[appDelegate teamRoster] addObject:fullName];
NSArray *temp = [[appDelegate teamRoster] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
[[appDelegate teamRoster] removeAllObjects];
[[appDelegate teamRoster] addObjectsFromArray:temp];
NSLog(@"%@", [appDelegate teamRoster]);
[ap
pDelegate.navigationController popViewControllerAnimated:YES];
}
Is there a better way? Depends on what you mean by “better”.
Your code is correct and easy enough to understand.
Assuming
teamRosteris a property of typeNSMutableArray, you could simply sort it in place:But there might be reasons NOT to do it that way, say for performance or concurrency.
On the other hand, you might want to protect the
teamRosterarray from being modified by code outside of the app delegate, in which case you should only make it available as an immutable NSArray, and you wouldn’t be able to use the above to sort it.