我编写了一个按姓氏字母顺序对 NSArray 进行排序的代码。我不知道如何让它一直跟随名字。有 strLName(姓氏)和 strFName(名字)。
我需要它看起来像这样(例如):
史蒂夫杰克逊蒂姆杰克逊艾伦琼斯黛安琼斯
目前它这样做:
蒂姆·杰克逊 史蒂夫·杰克逊 黛安·杰克逊 艾伦·杰克逊
到目前为止,这是我的代码:
{
[super viewDidLoad];
NSArray *arr = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", nil];
mArrSearched = [[NSMutableArray alloc] init];
for (int i=0; i<[AppDelegate sharedAppDelegate].arrGuests.count; i++)
{ President *pres = [[AppDelegate sharedAppDelegate].arrGuests objectAtIndex:i];
NSString *strLName = pres.strLName;
NSString *strAlpha = [NSString stringWithFormat:@"%c", [strLName characterAtIndex:0]];
NSString *str = [arr objectAtIndex:self.nIdx-1];
NSLog(@"LastName=%@, Alpha=%@, selectedStr=%@", strLName, strAlpha, str);
if ([strAlpha isEqualToString:[str uppercaseString]] || [strAlpha isEqualToString:str])
{
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:i], @"Index", strLName, @"Name", nil];
[mArrSearched addObject:dict];
}
}
[self sortByAlpha];
[tblView reloadData];
}
- (void) sortByAlpha {
NSSortDescriptor * frequencyDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
id obj;
if (mArrSearched.count > 0)
{
NSEnumerator * enumerator = [mArrSearched objectEnumerator];
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);
NSArray * descriptors = [NSArray arrayWithObjects:frequencyDescriptor, nil];
NSArray * sortedArray = [mArrSearched sortedArrayUsingDescriptors:descriptors];
NSLog(@"\nSorted ...");
enumerator = [sortedArray objectEnumerator];
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);
NSLog(@"%d", [[[mArrSearched objectAtIndex:0] objectForKey:@"Name"] intValue]);
mArrSearched = [sortedArray mutableCopy];
}
}