0

所以我在这里涉及三个不同的数组......

我正在循环通过 golferThreeIcons (NSMutableArray),我正在尝试获取最高的 4 个结果(golferThreeIcons 包含 # 作为字符串)并将它们存储到 topFourIconsNum (NSMutableArray)中,然后我计划存储最高的 id数组 topFourIconsId (NSMutableArray) 中的数字。

一件棘手的事情是 golferThreeIcons 从 99 开始,但我希望它像零一样。所以数字 99 不应该被添加到任何一个数组中......

示例: golferThreeIcons {2,1,5,3,9}

在循环完成后,我希望它显示类似这样的内容......

topFourIconsId {0,2,3,4} --- 0 对应 GolferThreeIcons 中的 2 --- 2 对应 Golfer 三个图标中的 5

topFourIconsNum {2,5,3,9} ----(任意顺序只要对应前四个图标id即可)

NSMutableArray *topFourIconsId = [NSMutableArray arrayWithObjects: @"0", @"0", @"0", @"0", @"0" ,nil];
NSMutableArray *topFourIconsNum = [NSMutableArray arrayWithObjects: @"0", @"0", @"0", @"0", @"0" ,nil];
int *ids = 0;
                for (NSString *s in golferThreeIconCounter) {
                    if(s != @"0") {
                        int sint = [s intValue];
                        int *idn = 0;
                        for(NSString *n in topFourIconsNum) {
                            int nint= [n intValue];
                            if(sint == 99 && nint == 0) {
                                NSString *idstring = [NSString stringWithFormat:@"%d", ids];
                                NSString *sintstring = [NSString stringWithFormat:@"%d", sint];
                                [topFourIconsId replaceObjectAtIndex:idn withObject:idstring];
                                [topFourIconsNum replaceObjectAtIndex:idn withObject:sintstring];
                                NSLog(@"IN %@",sintstring);
                                break;
                            }
                            else {
                                if (sint > nint) {
                                    NSString *idstring = [NSString stringWithFormat:@"%d", ids];
                                    NSString *sintstring = [NSString stringWithFormat:@"%d", sint];
                                    [topFourIconsId replaceObjectAtIndex:idn withObject:idstring];
                                    [topFourIconsNum replaceObjectAtIndex:idn withObject:sintstring];
                                    NSLog(@"IN %@",sintstring);
                                    break;
                                }
                            }
                            idn++;
                        }
                    }
                    ids++;
                }
4

1 回答 1

0

只是一个疯狂的想法

将您的 golferThreeIcons 放在一个 NSDictionary 中,其中数组索引作为键,数组值作为值,然后根据它们的值对键进行排序:

NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"2", @"1", @"5", @"3", @"9", nil] forKeys:[NSArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", nil]];
NSArray *sortedKeys = [dict keysSortedByValueUsingSelector:@selector(caseInsensitiveCompare:)];

NSLog([sortedKeys description]);
于 2012-05-23T11:31:49.070 回答