我有4个不同的团队,想要对它们进行降序排序,比较不同数组索引的值

时间:2014-03-04 19:04:46

标签: objective-c nsmutablearray

首先抱歉我的英语。我是Xcode的新程序员,我有一个问题:

我有4个不同的可变数组,我想对它们进行降序排序,比较它们在不同索引处的值,以查看团队位置。

我有阵列1,2,3,4

array1 [team1,6,0,0,0,0,0,0,4] 
array2 [team2,5,0,0,0,0,0,0,2] 
array3 [team3,5,0,0,0,0,0,0,3] 
array4 [team4,2,0,0,0,0,0,0,-3]  

排序后我要查找的输出是

array1 [team1,6,0,0,0,0,8,4,4] 
array3 [team3,5,0,0,0,0,4,3,3] 
array2 [team2,5,0,0,0,0,3,1,2] 
array4 [team4,2,0,0,0,0,1,-4,-3] 

首先,我想比较索引[1]的4个数组,看看哪个团队有更多的分数。

如果两支或更多球队在分数上并列,我想比较他们在指数[8]的目标差异。

如果球队有相同的净胜球,我想比较在指数[6]得分的目标 并按降序排列球队位置。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这似乎更容易使用词典

NSString * points = @"points";
NSString * goalDifference = @"goalDifference";
NSString * goalsScored = @"goalsScored";

NSDictionary * team1 = [[NSDictionary alloc]initWithObjectsAndKeys:
                        [NSNumber numberWithInt:6], points,
                        [NSNumber numberWithInt:4], goalDifference,
                        [NSNumber numberWithInt:8], goalsScored, nil];
NSDictionary * team2 = [[NSDictionary alloc]initWithObjectsAndKeys:
                        [NSNumber numberWithInt:5], points,
                        [NSNumber numberWithInt:3], goalDifference,
                        [NSNumber numberWithInt:4], goalsScored, nil];
NSDictionary * team3 = [[NSDictionary alloc]initWithObjectsAndKeys:
                        [NSNumber numberWithInt:5], points,
                        [NSNumber numberWithInt:2], goalDifference,
                        [NSNumber numberWithInt:3], goalsScored, nil];
NSDictionary * team4 = [[NSDictionary alloc]initWithObjectsAndKeys:
                        [NSNumber numberWithInt:2], points,
                        [NSNumber numberWithInt:-3], goalDifference,
                        [NSNumber numberWithInt:1], goalsScored, nil];

NSArray * sortedArray = [@[team1, team2, team3, team4] sortedArrayUsingComparator:^NSComparisonResult(NSDictionary * obj1, NSDictionary * obj2) {

    // are points equal?
    if ([obj1[points] intValue] != [obj2[points] intValue]) {

        // points not equal, compare points
        if ([obj1[points] intValue] > [obj2[points] intValue])
            return (NSComparisonResult)NSOrderedAscending;
        else
            return (NSComparisonResult)NSOrderedDescending;
    }
    else {

        // points equal, check goal difference
        if ([obj1[goalDifference] intValue] != [obj2[goalDifference] intValue]) {
            // goal difference not equal, compare goal difference
            if ([obj1[goalDifference] intValue] > [obj2[goalDifference] intValue])
                return (NSComparisonResult)NSOrderedAscending;
            else
                return (NSComparisonResult)NSOrderedDescending;
        }
        else {
            // goal difference & points equal - check goals scored
            if ([obj1[goalsScored] intValue] != [obj2[goalsScored] intValue]) {
                // goals scored not equal, compare goals scored
                if ([obj1[goalsScored] intValue] > [obj2[goalsScored] intValue])
                    return (NSComparisonResult)NSOrderedAscending;
                else
                    return (NSComparisonResult)NSOrderedDescending;
            }
            else {
                // points, goals scored, and goal difference are all equal
                return (NSComparisonResult)NSOrderedSame;
            }
        }
    }
}];

NSLog(@"RECEIVED: %@", sortedArray);

如果必须使用数组,请使用索引ex:

替换键
NSMutableArray * team1;
NSMutableArray * team2;
NSMutableArray * team3;
NSMutableArray * team4;

NSArray * sortedArray = [@[team1, team2, team3, team4] sortedArrayUsingComparator:^NSComparisonResult(NSMutableArray * obj1, NSMutableArray * obj2) {

    // are points equal?
    if ([obj1[1] intValue] != [obj2[1] intValue]) {

        // points not equal, compare points
        if ([obj1[1] intValue] > [obj2[1] intValue])
            return (NSComparisonResult)NSOrderedAscending;
        else
            return (NSComparisonResult)NSOrderedDescending;
    }
    else {

        // points equal, check goal difference
        if ([obj1[8] intValue] != [obj2[8] intValue]) {
            // goal difference not equal, compare goal difference
            if ([obj1[8] intValue] > [obj2[8] intValue])
                return (NSComparisonResult)NSOrderedAscending;
            else
                return (NSComparisonResult)NSOrderedDescending;
        }
        else {
            // goal difference & points equal - check goals scored
            if ([obj1[6] intValue] != [obj2[6] intValue]) {
                // goals scored not equal, compare goals scored
                if ([obj1[6] intValue] > [obj2[6] intValue])
                    return (NSComparisonResult)NSOrderedAscending;
                else
                    return (NSComparisonResult)NSOrderedDescending;
            }
            else {
                // points, goals scored, and goal difference are all equal
                return (NSComparisonResult)NSOrderedSame;
            }
        }
    }
}];

NSLog(@"RECEIVED: %@", sortedArray);