diff --git a/Classes/ChatConversationCreateTableView.m b/Classes/ChatConversationCreateTableView.m index aaa9cd5e9..ed1a66f14 100644 --- a/Classes/ChatConversationCreateTableView.m +++ b/Classes/ChatConversationCreateTableView.m @@ -15,6 +15,8 @@ @property(nonatomic, strong) NSMutableDictionary *contacts; @property(nonatomic, strong) NSDictionary *allContacts; +@property(nonatomic, strong) NSArray *sortedKeys; +@property(nonatomic, strong) NSArray *sortedAddresses; @end @implementation ChatConversationCreateTableView @@ -22,7 +24,18 @@ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.allContacts = - [[NSDictionary alloc] initWithDictionary:LinphoneManager.instance.fastAddressBook.addressBookMap]; + [[NSMutableDictionary alloc] initWithDictionary:LinphoneManager.instance.fastAddressBook.addressBookMap]; + self.sortedKeys = [[LinphoneManager.instance.fastAddressBook.addressBookMap allKeys] sortedArrayUsingSelector: @selector(compare:)]; + + self.sortedAddresses = [[LinphoneManager.instance.fastAddressBook.addressBookMap allKeys] sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { + Contact* first = [_allContacts objectForKey:a]; + Contact* second = [_allContacts objectForKey:b]; + + if([[first.firstName lowercaseString] compare:[second.firstName lowercaseString]] == NSOrderedSame) + return [[first.lastName lowercaseString] compare:[second.lastName lowercaseString]]; + else + return [[first.firstName lowercaseString] compare:[second.firstName lowercaseString]]; + }]; self.contacts = [[NSMutableDictionary alloc] initWithCapacity:_allContacts.count]; [_searchBar becomeFirstResponder]; [_searchBar setText:@""]; @@ -33,15 +46,14 @@ - (void)reloadDataWithFilter:(NSString *)filter { [_contacts removeAllObjects]; - [_allContacts enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { - NSString *address = (NSString *)key; - NSString *name = [FastAddressBook displayNameForContact:value]; - if ((filter.length == 0) || ([name.lowercaseString containsSubstring:filter.lowercaseString]) || - ([address.lowercaseString containsSubstring:filter.lowercaseString])) { - _contacts[address] = name; - } - - }]; + for (NSString* key in _sortedAddresses){ + NSString *address = (NSString *)key; + NSString *name = [FastAddressBook displayNameForContact:[_allContacts objectForKey:key]]; + if ((filter.length == 0) || ([name.lowercaseString containsSubstring:filter.lowercaseString]) || + ([address.lowercaseString containsSubstring:filter.lowercaseString])) { + [_contacts setObject:name forKey:address] ; + } + } // also add current entry, if not listed NSString *nsuri = filter.lowercaseString; LinphoneAddress *addr = [LinphoneUtils normalizeSipOrPhoneAddress:nsuri]; @@ -72,8 +84,8 @@ if (cell == nil) { cell = [[UIChatCreateCell alloc] initWithIdentifier:kCellId]; } - cell.displayNameLabel.text = [_contacts.allValues objectAtIndex:indexPath.row]; - LinphoneAddress *addr = [LinphoneUtils normalizeSipOrPhoneAddress:[_contacts.allKeys objectAtIndex:indexPath.row]]; + LinphoneAddress *addr = [LinphoneUtils normalizeSipOrPhoneAddress:[_sortedAddresses objectAtIndex:indexPath.row]]; + cell.displayNameLabel.text = [_contacts objectForKey:[_sortedAddresses objectAtIndex:indexPath.row]]; if (addr) { cell.addressLabel.text = [NSString stringWithUTF8String:linphone_address_as_string(addr)]; } else { @@ -85,7 +97,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSString *uri; - LinphoneAddress *addr = [LinphoneUtils normalizeSipOrPhoneAddress:[_contacts.allKeys objectAtIndex:indexPath.row]]; + LinphoneAddress *addr = [LinphoneUtils normalizeSipOrPhoneAddress:[_sortedAddresses objectAtIndex:indexPath.row]]; if (addr) { uri = [NSString stringWithUTF8String:linphone_address_as_string(addr)]; } else {