Ios8 如何对数组中对象的所有索引进行排序

Ios8 如何对数组中对象的所有索引进行排序,ios8,Ios8,我正在使用这行代码;我的数组包含姓名、电子邮件和电话号码。此代码仅按姓名排序,但我希望电子邮件和电话号码包含姓名。我的数组separenesbyletters包含带有名称的电子邮件和电话,我如何才能做到这一点 NSMutableSet *firstCharacters = [NSMutableSet setWithCapacity:0]; for( NSString*string in [ tableDataArray valueForKey:@"name"] ){ [

我正在使用这行代码;我的数组包含姓名、电子邮件和电话号码。此代码仅按姓名排序,但我希望电子邮件和电话号码包含姓名。我的数组separenesbyletters包含带有名称的电子邮件和电话,我如何才能做到这一点

 NSMutableSet *firstCharacters = [NSMutableSet setWithCapacity:0];
    for( NSString*string in [ tableDataArray valueForKey:@"name"] ){
        [firstCharacters addObject:[[string substringToIndex:1] uppercaseString]];
    }
    NSArray *allLetters = [[firstCharacters allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    int indexLetter = 0;
    separeNamesByLetters = [NSMutableArray new];



    for (NSString *letter in allLetters) {
        NSMutableDictionary*userBegeinsWith = [NSMutableDictionary new];
        [userBegeinsWith setObject:letter forKey:@"letter" ];
        NSMutableArray *groupNameByLetters = [NSMutableArray new];

        NSString *compareLetter1 = [NSString stringWithFormat:@"%@", allLetters[indexLetter]];
        for (NSString*friendName in[ tableDataArray valueForKey:@"name"]) {
            NSString *compareLetter2 = [[friendName substringToIndex:1] uppercaseString];

            if ( [compareLetter1 isEqualToString:compareLetter2] ) {
                [groupNameByLetters addObject:friendName];
            }
        }
        indexLetter++;
        [userBegeinsWith setObject:groupNameByLetters forKey:@"list"];
        [separeNamesByLetters addObject: userBegeinsWith];
    }



    NSLog(@"%@", separeNamesByLetters);


}

您是否考虑过使用NSSORTDescriptor?请参见以下示例:

NSMutableArray *array = [NSMutableArray new];

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Doe, John", @"name", @"3361231234", @"phone", @"johndoe@email.com", @"email", nil];
[array addObject:dict];

dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Doe, Jane", @"name", @"9191234532", @"phone", @"janedoe@email.com", @"email", nil];
[array addObject:dict];

NSSortDescriptor *nameDescriptor =
[[NSSortDescriptor alloc]
  initWithKey:@"name"
  ascending:YES
  selector:@selector(localizedCaseInsensitiveCompare:)];

NSSortDescriptor *phoneDescriptor =
[[NSSortDescriptor alloc]
  initWithKey:@"phone"
  ascending:YES
  selector:@selector(localizedCaseInsensitiveCompare:)];

NSSortDescriptor *emailDescriptor =
[[NSSortDescriptor alloc]
 initWithKey:@"email"
 ascending:YES
 selector:@selector(localizedCaseInsensitiveCompare:)];

NSArray * descriptors = [NSArray arrayWithObjects:nameDescriptor, phoneDescriptor, emailDescriptor, nil];
NSArray * sortedArray = [array sortedArrayUsingDescriptors:descriptors];

让我知道这是否有用。

当我这样做时,我会创建一个新的NSArray NSDictionary。NSDictionary有两个属性:sectionTitle和records。sectionTitle包含您正在搜索的字段的第一个字母。记录包含以该节标题开头的实际对象

将此sectionTitle方法添加到模型类中。我通常在实际模型类的扩展中这样做

- (NSString *)sectionTitle:(NSString*)sortByProperty {

    NSString *propertyValue;

    if ([sortByProperty isEqualToString:@"name"]) {
        if (self.name == nil) return @"";
        propertyValue = self.name;
    } else if ([sortByProperty isEqualToString:@"email"]) {
        if (self.email == nil) return @"";
        propertyValue = self.phone;
    } else if ([sortByProperty isEqualToString:@"phone"]) {
        if (self.phone == nil) return @"";
        propertyValue = phone
    }

    NSString *tmp;
    if ([propertyValue length] > 0) {
        tmp = [propertyValue substringToIndex:1];
    } else {
        return @"";
    }

    return tmp;
}
这应该比每次刷新tableView时遍历数组更快

接下来,迭代这些节标题,过滤数组,并将过滤结果添加到字典的records属性中:

- (NSMutableArray*)sortedArrayForUITableView:(NSMutableArray *)tableDataArray sortBy:(NSString*)sortByProperty {

    NSArray *tmp = [tableDataArray valueForKeyPath:@"@distinctUnionOfObjects.sectionTitle"];
    NSArray *allLetters = [NSMutableArray arrayWithArray:[tmp sortedArrayUsingDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]]]];

    NSMutableArray *tree = [NSMutableArray new];
    for (NSString *sectionTitle in allLetters) {

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.sectionTitle == %@", sectionTitle];
        NSMutableArray *tmpArray = [NSMutableArray arrayWithArray:[tableDataArray filteredArrayUsingPredicate:predicate]];
        NSArray *sortedArray = [tmpArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:sortByProperty ascending:YES]]];

        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:sectionTitle, @"sectionTitle", sortedArray, @"records", nil];

        [tree addObject:dict];

    }
    return tree;
}
您需要实现几个UITableViewDataSource和UITableViewDelegate方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.tree count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dict = [NSDictionary dictionaryWithDictionary:[self.tree objectAtIndex:section]];
    NSArray *tmp = [NSArray arrayWithArray:[dict objectForKey:@"records"]];

    return [tmp count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSDictionary *dict = [NSDictionary dictionaryWithDictionary:[self.tree objectAtIndex:section]];
    return [dict objectForKey:@"sectionTitle"];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.allLetters;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return index;
}

最后,您将在
-(UITableViewCell*)tableView:(UITableView*)tableView cell for rowatinexpath:(nsindepath*)indepath
中构建单元格。希望这能有所帮助。

您的代码运行良好,但我的问题是如何处理A部分所接触的数组,以A开头,以此类推。。如何在tableviewhi.部分中使用它。。mrbcg,其中我调用此方法-(NSMutableArray*)SortDarrayForUITableView:(NSMutableArray)tableDataArray sortBy:(NSString)sortBy:(NSString)sortByProperty{以及我调用此方法的地方-(NSString)sectionTitle:(NSString)sortByProperty{我无法使用此代码,请在此处提及我可以使用它的地方