您可以使用“排序”的NSMutableArray,它会牵着你的27“信”的NSMutableArray对象每持有你的对象。
您也可以使用26个字母的NSArray(作为NSString对象),并使用indexOfObject:
从对象应该添加到的“letter”数组的“sorted”数组中获取索引(如果它返回NSNotFound,使用26作为索引,因为那是你的非alpha数组)。
例如,如果要排序NSString对象,你可以做这样的:
NSArray *originalArray = [NSArray arrayWithObjects:@"A object",@"B object",@"C object",@"123 object",nil];
// init with all 26 lowercase letters here
NSArray *letters = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];
// create sorted and letter arrays
NSMutableArray *sortedArray = [[NSMutableArray alloc] initWithCapacity:[letters count]+1];
for (int i = 0; i < [letters count] + 1; i++) {
// use an appropriate capacity here
NSMutableArray *letterArray = [[NSMutableArray alloc] initWithCapacity:10];
[sortedArray addObject:letterArray];
[letterArray release];
}
// sort originalArray into sortedArray
for (NSString *string in originalArray) {
NSString *firstLetter = [[string substringWithRange:[string rangeOfComposedCharacterSequenceAtIndex:0]] lowercaseString];
int index = [letters indexOfObject:firstLetter];
if (index == NSNotFound) {
// use non-alpha array
index = [letters count];
}
NSMutableArray *letterArray = [sortedArray objectAtIndex:index];
[letterArray addObject:string];
}
NSLog(@"%@",sortedArray);
非常感谢的是,工作一种享受。 – Affian 2010-02-03 21:37:46