我有一个使用地址簿的应用。我正在尝试使用
显示地址簿中的已排序名称列表sortedArray = [arr_contactList sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
然后当用户选择其中一个联系人时,会显示其电话号码。
我可以对iPhone地址簿电话号码进行排序。
我使用以下方法对电话号码进行排序:
ABRecordRef source = ABAddressBookCopyDefaultSource(ab);
NSArray *thePeople = (NSArray*)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(ab, source, kABPersonSortByFirstName);
NSString *name;
for (id person in thePeople)
{
name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
NSString* num = (NSString*)ABMultiValueCopyValueAtIndex(phones, j);
CFStringRef locLabel1 = ABMultiValueCopyLabelAtIndex(phones, j);
NSString *phoneLabel1 =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel1);
[tempPhoneArray addObject:num];
}
}
但我的实际问题是,我的名字数组的联系人以列表顶部的特殊字符开头,当我选择电话号码时,联系人列表以字母A开头。所以我的电话号码错了。< / p>
如何匹配这两种排序 - 名称排序和数字排序?
答案 0 :(得分:1)
在这个例子中,你将制作27个数组,每个字母1个,但这个概念可以应用于特殊字符vs大/小写的任何检查。希望这会有所帮助。
const int capacity = 27;
NSMutableArray *subArrays = [NSMutableArray array];
//Prepopulate the subArray with 26 empty arrays
for(int i = 0; i < capacity; i++)
[subArrays addObject:[NSMutableArray array]];
char currFirstLetter = 'a';
for(int i = 0; i < sortedArray.count; i++)
{
NSString *currString = [[sortedArray objectAtIndex:i] lowercaseString];
NSLog(@"%@", currString);
NSLog(@"%c", [currString characterAtIndex:0]);
NSLog(@"%c", currFirstLetter);
if([currString characterAtIndex:0] == currFirstLetter)
{
//65 is the position of 'a' in the ascii table, so when we subtract 97, it correlates to 0 in our array.
[[subArrays objectAtIndex:currFirstLetter-97] addObject:[sortedArray objectAtIndex:i]];
}
else if([currString characterAtIndex:0] < 65 || ([currString characterAtIndex:0] > 90 && [currString characterAtIndex:0] < 97) || [currString characterAtIndex:0] > 122)
{
//If it's a symbol (65-90 are uppercase, 97-122 are lowercase)
[[subArrays objectAtIndex:26] addObject:[sortedArray objectAtIndex:i]];
//Increment the letter we're looking for, but decrement the count to try it again with the next letter
}
else
{
currFirstLetter++;
i--;
}
}