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

时间:2015-04-13 12:44:35

标签: ios8

我正在使用这行代码;我的数组包含姓名,电子邮件和电话号码。此代码仅按名称排序,但我希望电子邮件和电话号码没有名称。我怎么能这样做我的数组separeNamesByLetters包含电子邮件和名字的电话?

 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);


}

2 个答案:

答案 0 :(得分:0)

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

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];

如果有帮助,请告诉我。

答案 1 :(得分:0)

当我这样做时,我创建了一个新的NSArray NSArray。 NSDictionary有两个属性:sectionTitle和records。 sectionTitle包含您要搜索的字段的第一个字母。记录包含以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时,这应该比遍历数组更快。

接下来,遍历这些sectionTitles,过滤你的数组,并将过滤后的结果添加到字典的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 cellForRowAtIndexPath:(NSIndexPath *)indexPath中构建您的单元格。希望有所帮助。