【发布时间】:2011-06-20 18:07:37
【问题描述】:
我这里有两个数组来自另一个类,一个是我在函数中创建的,我的最终目标是让 teamRoster 按字母顺序排序,这似乎可行,但有人可以告诉我是否有更好的方法吗?谢谢。
-(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];
}
【问题讨论】:
-
您想在正确的排序位置插入您的新全名吗?而不是使用插入的新全名重新排序整个数组。如果您在谈论数组中的数百或数千个元素,这确实是优化的,您可以首先使用二进制搜索确定位置,然后在该位置插入。
标签: iphone objective-c ios xcode nsarray