阵列#1中最高的4个数字存储在另一个阵列中

时间:2012-05-23 11:16:15

标签: iphone objective-c ios cocoa-touch ios5

所以我在这里涉及三个不同的阵列......

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

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

实施例: golferThreeIcons {2,1,5,3,9}

循环结束后,我希望它显示出类似的东西......

topFourIconsId {0,2,3,4} --- 0对应于golferThreeIcons中的2 --- 2对应于高尔夫球手中的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++;
                }

1 个答案:

答案 0 :(得分: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]);