在我的应用程序中,我有一个包含两个数组的字典,例如:
NSDictionary *d=[[NSDictionary alloc]initWithObjectsAndKeys:listOfEvents,@"l",arr_distance,@"d", nil];
NSLog(@"%@",d);
d = (
"353.90",
"354.68",
"350.42",
"1.18"
);
l = (
{
Contestant1 = "Jon jones";
Contestant2 = "Anderson silva";
},
{
Contestant1 = "Jon jones";
Contestant2 = "Anderson silva";
},
{
Contestant1 = "Jon jones";
Contestant2 = "Anderson silva";
},
{
Contestant1 = "Jon jones";
Contestant2 = "Anderson silva";
}
);
}
我想用第一个数组对字典进行排序,当索引更改时,第二个数组索引也随之更改。
两个数组索引相互关联。
@ABC 你尝试过的代码
NSDictionary *dict = [NSDictionary dictionaryWithObjects:listOfEvents forKeys:arr_distance];
NSArray *sortedArray = [arr_distance sortedArrayUsingSelector:@selector(compare:)];
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedArray) {
[sortedValues addObject:[dict valueForKey:key]];
}
NSLog(@"%@",sortedValues);
结果:
{ 1.18,2563.91,2563.41,2563.39,350,353,354}