1

我被困在这里......我有两个数组 Arr_title 和 Arr_distance 我使用这种方法对 Arr_distance 进行了排序

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]autorelease];

sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
                             [NSArray arrayWithObject:sortDescriptor]];

但问题是我想根据 sortedFloats 数组的索引对 Arr_title 进行排序...

thx 提前帮助我.......

4

4 回答 4

1

尝试这个,

NSDictionary *dict = [NSDictionary dictionaryWithObjects:Arr_title forKeys:Arr_distance];

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES] autorelease];
sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
                         [NSArray arrayWithObject:sortDescriptor]];


NSMutableArray *sortedTitles = [NSMutableArray array];

for (NSString *key in sortedFloats) {//or just use sortedTitles = [dict objectsForKeys:sortedFloats notFoundMarker:[NSNull null]];
    [sortedTitles addObject:[dict valueForKey:key]]; 
}

NSLog(@"%@",sortedValues);

sortedTitles应该为您提供按相同顺序排序的数组sortedFloats

于 2012-12-07T06:16:24.857 回答
1

嘿,而不是采用两个不同的数组,而是用 1 个带 title,distance的字典将该字典保存在数组中,然后对该数组进行排序所以你已经组合在一起了,所以由于任何排序的映射(按标题和按距离)都没有任何问题。

于 2012-12-07T05:54:28.490 回答
0

我得到了解决方案,请参见以下代码......

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]autorelease];

sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
                             [NSArray arrayWithObject:sortDescriptor]];

 NSLog(@" Sorted Array : %@", sortedFloats);

 NSDictionary *dictTitle = [NSDictionary dictionaryWithObjects:Arr_title   forKeys:appDel.Arr_distance];

// Sort the second array based on the sorted first array
sortedTitle = [dictTitle objectsForKeys:sortedFloats notFoundMarker:[NSNull null]];


NSLog(@" Sorted Title : %@",sortedTitle);


// Put the two arrays into a dictionary as keys and values
NSDictionary *dictAddress = [NSDictionary dictionaryWithObjects:Arr_address   forKeys:appDel.Arr_distance];

// Sort the second array based on the sorted first array
sortedAddress = [dictAddress objectsForKeys:sortedFloats notFoundMarker:[NSNull null]];


NSLog(@" Sorted Address : %@",sortedAddress);

如果在其他方法中使用,请不要忘记保留您的数组....

于 2012-12-07T07:44:52.893 回答
0

如果我理解你的问题是正确的,你可以使用 NSDictionary 来完成同样的任务

NSMutableArray *array = [NSMutableArray arrayWithArray:@[ @{@"title" : @"City1",@"dist" : @5},

                             @ {@"title" : @"City2",@"dist" : @2.3 },
                             @{@"title" : @"City3",@"dist" : @11.0 },
                             @{@"title" : @"City4",@"dist" : @1.0},
                             @{@"title" : @"City5",@"dist" : @.5},
                             @{@"title" : @"City6",@"dist" : @13 }]];

NSLog(@"dict<%@>",array);

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"dist" ascending:YES];

NSArray *sortarray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

NSLog(@"sort array = <%@>",sortarray);
于 2012-12-07T06:56:32.160 回答