iOS - 使用相同的顺序从多个阵列中获取唯一值

时间:2016-07-24 08:57:49

标签: ios objective-c nsarray

我有2个阵列:

数组一:(@"26", @"26", @"25", @"25", @"3", @"3", @"4", @"4")

数组二:(@"sticker", @"sticker", @"sticker", @"sticker", @"sticker", @"frame", @"frame", @"frame")

修改

这两个数组连接如下:

26 - sticker
26 - sticker
25 - sticker
25 - sticker
3 - frame
3 - frame
4 - frame
4 - frame

我在数组1中获得了独特的价值并将其放入另一个数组中:

NSArray *uniqueArrayOne = [[NSSet setWithArray:arrayOne] allObjects];

所以uniqueArrayOne看起来像这样:

(26, 25, 3, 4)

我希望uniqueArrayTwo也像uniqueArrayOne的安排一样安排。我希望uniqueArrayTwo看起来像这样:

(sticker, sticker, frame, frame)

我该怎么办?

修改

这是另一个例子:

数组一:(@"TONY", @"SANSA", @"BILL", @"BILL", @"STEVE", @"STEVE")

数组二:(@"STARK", @"STARK", @"GATES", @"GATES", @"JOBS", @"JOBS")

结果应该是这样的:

uniqueArrayOne:(TONY, SANSA, BILL, STEVE)

uniqueArrayTwo:(STARK, STARK, GATES, JOBS)

uniqueArrayTwo的安排取决于uniqueArrayOne

3 个答案:

答案 0 :(得分:1)

我会这样解决

beforeSortChanged

输出:

- (void)uniqueArrayFinder
{
    NSArray *array1 = [[NSArray alloc] initWithObjects:
                       @"TONY", @"SANSA", @"BILL", @"BILL", @"STEVE", @"STEVE",nil];
    NSArray *array2 = [[NSArray alloc] initWithObjects:
                       @"STARK", @"STARK", @"GATES", @"GATES", @"JOBS", @"JOBS",nil];

    NSMutableArray *combinedArray = [[NSMutableArray alloc] init];

    for (int i=0; i<[array1 count];i++)
    {
        NSString *arrayVal1 = [array1 objectAtIndex:i];
        NSString *arrayVal2 = [array2 objectAtIndex:i];
        NSString *combinedStr = [NSString stringWithFormat:@"%@ %@", arrayVal1, arrayVal2];
        [combinedArray addObject:combinedStr];
    }

    //this gives uniqe values
    NSSet *uniqueEvents = [NSSet setWithArray:combinedArray];
    [combinedArray removeAllObjects];
    [combinedArray addObjectsFromArray:[uniqueEvents allObjects]];

    NSLog(@"combinedArray: %@ ...", combinedArray);


}

答案 1 :(得分:1)

您可以通过以下方式获得所需的结果,

NSArray *arr1 = @[@"26", @"26", @"25", @"25", @"3", @"3", @"4", @"4"];
NSArray *arr2 = @[@"sticker", @"sticker", @"sticker", @"sticker", @"sticker", @"frame", @"frame", @"frame"];

NSDictionary *dict = [NSDictionary dictionaryWithObjects:arr2 
                                   forKeys:arr1];

NSArray *distinctArr1 = [[NSOrderedSet orderedSetWithArray:arr1] array];

NSMutableArray *distinctArr2 = [NSMutableArray array];
for (NSString *num1 in distinctArr1) {
    [distinctArr2 addObject:dict[num1]];
}
// Your distinctArr2 is sorted based on distinctArr1's indices

答案 2 :(得分:0)

试试这个。

NSArray *arrayOne = @[@"TONY", @"SANSA", @"BILL", @"BILL", @"STEVE", @"STEVE"];
NSArray *arrayTwo = @[@"STARK", @"STARK", @"GATES", @"GATES", @"JOBS", @"JOBS"];

NSArray *uniqueArrayOne = [[NSSet setWithArray:arrayOne] allObjects];
NSMutableArray *uniqueArrayTwo = [NSMutableArray array];

[uniqueArrayOne enumerateObjectsUsingBlock:^(id  _Nonnull obj1, NSUInteger idx, BOOL * _Nonnull stop) {
    NSUInteger found = [arrayOne indexOfObjectPassingTest:^BOOL(id  _Nonnull obj2, NSUInteger idx, BOOL * _Nonnull stop) {
        return [(NSString *)obj2 isEqualToString:(NSString *)obj1];
    }];
    [uniqueArrayTwo addObject:[arrayTwo objectAtIndex:found]];
}];

NSLog(@"%@, %@", uniqueArrayOne, uniqueArrayTwo);