并排排序2 NSArrays在一起边 [英] Sorting two NSArrays together side by side

查看:112
本文介绍了并排排序2 NSArrays在一起边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要并排排序侧多个数组。

例如,第一阵列的名称: @ [@乔,@安娜,@迈克尔,@金] ,和
另一个数组保存地址: @ [@好莱坞BLD,@一些街道3,@其他街道,@乡村公路] ,其中阵列指标一起去。 乔住在好莱坞BLD等。

我想的名字按字母顺序排列排序,然后有地址阵列排列一起,使他们仍然走在一起,好莱坞BLD有相同的索引乔。我知道如何排序一个数组的字母

  NSSortDescriptor *排序= [NSSortDescriptor sortDescriptorWithKey:@名升:NO];
[myArray的sortUsingDescriptors:[NSArray的arrayWithObject:排序]];

但有越来越采用适当的顺序第二个数组排序任何简单的方法?


解决方案

  1. 创建一个排列阵列,最初设置为 P [i] = I

  2. 排序置换是第一组的名称

  3. 使用置换重新排序两个数组

例如:让我们说第一个数组是 {快,棕色,狐狸精} 。置换开始为 {0,1,2} ,并成为 {1,2,0} 排序后。现在,你可以通过置换阵列,并重新排序原阵列和第二阵列需要。

 的NSArray *第一= [NSArray的arrayWithObjects:@快,@棕色,@狐狸,@跳跃,零]
NSArray的*秒= [NSArray的arrayWithObjects:@杰克,@爱,@我的,@狮身人面像零]
NSMutableArray里* P = [NSMutableArray的arrayWithCapacity:first.count];
对于(NSUInteger我!= 0; i = first.count;我++){
    [P ADDOBJECT:[NSNumber的numberWithInteger:我]];
}
[P sortWithOptions:0 usingComparator:^ NSComparisonResult(ID OBJ1,ID OBJ2){
    //修改该使用[第一objectAtIndex:[OBJ1的intValue]。name属性
    * NSString的LHS = [第一objectAtIndex:[OBJ1的intValue];
    //同样适用于下一行:使用名称
    * NSString的RHS = [第一objectAtIndex:[OBJ2的intValue];
    返回[LHS比较:RHS];
}];
NSMutableArray里* sortedFirst = [NSMutableArray的arrayWithCapacity:first.count];
NSMutableArray里* sortedSecond = [NSMutableArray的arrayWithCapacity:first.count];
[P enumerateObjectsUsingBlock:^(ID OBJ,NSUInteger IDX,BOOL *停止){
    NSUInteger POS = [OBJ的intValue];
    [sortedFirst ADDOBJECT:[第一objectAtIndex:POS]];
    [sortedSecond ADDOBJECT:[第二objectAtIndex:POS]];
}];
的NSLog(@%@,sortedFirst);
的NSLog(@%@,sortedSecond);

I have several arrays that need to be sorted side by side.

For example, the first array has names: @[@"Joe", @"Anna", @"Michael", @"Kim"], and and the other array holds addresses: @[@"Hollywood bld", @"Some street 3", @"That other street", @"country road"], where the arrays' indexes go together. "Joe" lives at "Hollywood bld" and so on.

I would like to sort the names array alphabetically, and then have the address array sorted alongside so they still go together, with "Hollywood bld" having same index as "Joe". I know how to sort one array alphabetical with

NSSortDescriptor *sort=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];

But is there any easy way of getting the second array sorted using the appropriate order?

解决方案

  1. Create a permutation array, initially set to p[i]=i
  2. Sort the permutation according to the name key of the first array
  3. Use the permutation to re-order both arrays

Example: let's say the first array is {"quick", "brown", "fox"}. The permutation starts as {0, 1, 2}, and becomes {1, 2, 0} after the sort. Now you can go through the permutation array, and re-order the original array and the second array as needed.

NSArray *first = [NSArray arrayWithObjects: @"quick", @"brown", @"fox", @"jumps", nil];
NSArray *second = [NSArray arrayWithObjects: @"jack", @"loves", @"my", @"sphinx", nil];
NSMutableArray *p = [NSMutableArray arrayWithCapacity:first.count];
for (NSUInteger i = 0 ; i != first.count ; i++) {
    [p addObject:[NSNumber numberWithInteger:i]];
}
[p sortWithOptions:0 usingComparator:^NSComparisonResult(id obj1, id obj2) {
    // Modify this to use [first objectAtIndex:[obj1 intValue]].name property
    NSString *lhs = [first objectAtIndex:[obj1 intValue]];
    // Same goes for the next line: use the name
    NSString *rhs = [first objectAtIndex:[obj2 intValue]];
    return [lhs compare:rhs];
}];
NSMutableArray *sortedFirst = [NSMutableArray arrayWithCapacity:first.count];
NSMutableArray *sortedSecond = [NSMutableArray arrayWithCapacity:first.count];
[p enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSUInteger pos = [obj intValue];
    [sortedFirst addObject:[first objectAtIndex:pos]];
    [sortedSecond addObject:[second objectAtIndex:pos]];
}];
NSLog(@"%@", sortedFirst);
NSLog(@"%@", sortedSecond);

这篇关于并排排序2 NSArrays在一起边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆