根据已排序的另一个数组的顺序调整一个数组的顺序

时间:2016-08-09 01:10:22

标签: ios objective-c arrays sorting indexing

我有两个表,一个表用于用户名,一个表用于分数。我用两个数组填充这些表。表格需要根据分数降序排列。我对数组中的分数进行排序,但我不确定如何安排用户名来坚持他们的分数,他们在一个单独的数组中,以及一个与分数表分开的表。这是我的代码:

dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [dictionary allKeys];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
sortedSecondArray = [sortedSecondArray sortedArrayUsingSelector: @selector(compare:)];

我需要sortedFirstArray值来确定各自sortedSecondArray值在每个数组中的顺序。

更新

我的代码尝试进行排序:

PFQuery *query = [PFQuery queryWithClassName:@"_User"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (!error) {
                entries = [NSMutableArray new];
                for (PFObject *object in objects) {
                    NSLog(@"%@", object.objectId);
                    [tableData addObject:[object valueForKey:@"username"]];
                    [matchesForUser addObject:[object valueForKey:@"matches"]];

                    NSMutableDictionary* entry = [NSMutableDictionary new];

                    entry[@"username"] = [object valueForKey:@"username"];
                    entry[@"matches"] = [object valueForKey:@"matches"];

                    [entries addObject:entry];

                    //transfer = entries;
                }
                transfer = [entries sortedArrayUsingComparator:^NSComparisonResult(NSDictionary* a, NSDictionary* b) {
                    NSDate *first  = [a objectForKey:@"matches"];
                    NSDate *second = [b objectForKey:@"matches"];
                    NSLog(first);
                    NSLog(second);
                    return [first compare:second];
                }];
            //dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
            //sortedFirstArray = [dictionary allKeys];
            //sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
            //sortedSecondArray = [sortedSecondArray sortedArrayUsingSelector: @selector(compare:)];
            [_tableView reloadData];
            [_tableViewScore reloadData];

        }else{
            NSLog([error description]);
        }

        NSLog(@"***tabledata***");
        NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]);
        NSLog(@"***matchesdata***");
        NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]);
    });
}];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView.tag == 1) {
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0];
    cell.textLabel.textColor = [UIColor colorWithRed:218.0f/255.0f green:247.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
    cell.backgroundColor = [UIColor colorWithRed:153.0f/255.0f green:211.0f/255.0f blue:212.0f/255.0f alpha:1.0f];
    cell.layoutMargins = UIEdgeInsetsZero;
    cell.preservesSuperviewLayoutMargins = NO;

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    UILabel *contentV = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 230, 44)];
    contentV.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0];
    contentV.textColor = [UIColor colorWithRed:218.0f/255.0f green:247.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
    contentV.backgroundColor = [UIColor colorWithRed:153.0f/255.0f green:211.0f/255.0f blue:212.0f/255.0f alpha:1.0f];

    cell.contentView.layoutMargins = UIEdgeInsetsZero;

    NSString *username2 = [[transfer objectAtIndex:indexPath.row] valueForKey:@"username"];

    NSLog(@"***username***");
    NSLog(username2);

    contentV.text = username2;
    [cell.contentView addSubview:contentV];

    return cell;
}
else {
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0];
    cell.textLabel.textColor = [UIColor colorWithRed:153.0f/255.0f green:211.0f/255.0f blue:212.0f/255.0f alpha:1.0f];
    cell.backgroundColor = [UIColor colorWithRed:218.0f/255.0f green:247.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    NSString *matchAmount = [[transfer objectAtIndex:indexPath.row] valueForKey:@"matches"];
    NSLog(@"***matchamount***");
    NSLog(matchAmount);

    cell.textLabel.text = matchAmount;

    return cell;

}
}

2 个答案:

答案 0 :(得分:2)

而不是有两个单独的数组,并且直接排序其中一个数据并尝试找出另一个数据中哪些条目对应于已排序的数组中的哪一个,而应该将两个数组的配对条目保持在一起一个实体(自定义类的字典或实例),并将(单个)数组中的那些组合在一起:

实际的属性名称等因代码而异,但一般的想法是这样的:

self.entries = [NSMutableArray new];

for (int i=0; i < userNameArray.count; i++){

    NSMutableDictionary* entry = [NSMutableDictionary new];

    entry["userName"] = [userNameArray objectAtIndex: i];
    entry["score"   ] = [scoreArray objectAtIndex: i]; 
    // ^ TODO: Make sure scoreArray has at least as many elements 
    // as userNameArray!

    [self.entries addObject: entry];
}

self.entries = [self.entries sortedArrayUsingComparator:^NSComparisonResult(NSDictionary* a, NSDictionary* b) {
    NSDate *first  = [a objectForKey:"score"];
    NSDate *second = [b objectForKey:"score"];
    return [first compare:second];
}];

// (...)

UITableViewCell* tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath}) indexPath
{
    NSDictionary* entry = [self.entries objectAtIndex: indexPath.row];

    if (tableView == self.userNameTableView) {
         // (dequeue user cell...)

         cell.titleLabel.text = [entry objectForKey: "userName"];
         return cell
     }
     else{
         // (dequeue score cell...)

         cell.titleLabel.text = [entry objectForKey: "score"];
         return cell
     }
}

答案 1 :(得分:1)

NicolasMiari帮助我解决了大部分问题。比较的方式发生了一些奇怪的事情 - 它产生了一个未分类的结果。这是我的排序方式:

NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"matches" ascending:NO selector:@selector(localizedStandardCompare:)];
NSArray *entrieshold = [entries sortedArrayUsingDescriptors:@[descriptor]];
transfer = [entrieshold copy];

对我来说,最重要的事情似乎是selector:@selector(localizedStandardCompare:)。我对copy的使用也很重要......但我不这么认为。