-1

我有一个二维数组如下

NSArray *cities = [NSArray arrayWithObjects:@"New Delhi",@"Karachi",@"Dhaka",@"Columbu", nil];
NSArray *distance = [NSArray arrayWithObjects:@"500",@"1400",@"1200",@"2800", nil];
NSMutableArray *details= [[NSMutableArray alloc]initWithCapacity:cities.count];
[details addObject:cities];
[details addObject:distance];

现在,details数组看起来像

(
    (
    "New Delhi",
    Karachi,
    Dhaka,
    Columbu
 ),
    (
    500,
    1400,
    1200,
    2800
 )
)

我需要使用升序对数组进行排序 wrtdistance数组,即

(
    (
    "New Delhi",
    Dhaka,
    Karachi,
    Columbu
 ),
    (
    500,
    1200,
    1400,
    2800
 )
)

这个怎么做?

我还需要按字母顺序对整个数组进行排序 wrtcities数组。

我尝试使用sortUsingComparator,但无法完全获得解决方案。

任何帮助,将不胜感激。

4

2 回答 2

1

结构复杂。city创建一个包含nameand的类distance。然后添加这个类的对象。然后您可以使用city.distance.

于 2013-06-12T09:38:36.197 回答
1
self.arrayForRows = [[NSMutableArray alloc]init];
NSMutableArray *arrayForCities = [[NSMutableArray alloc]initWithObjects:@"Mumbai",@"Vizag",@"Hyderabad",@"Ahemdabad",@"Secunderabad", nil];
    NSMutableArray *arrayForDistance = [[NSMutableArray alloc]initWithObjects:@"200",@"320",@"32",@"450",@"14", nil];

    for (int i=0; i<arrayForCities.count; i++)
    {
        NSMutableDictionary *tempDicts = [[NSMutableDictionary alloc]init];
        [tempDicts setObject:[arrayForCities objectAtIndex:i] forKey:@"names"];
        [tempDicts setObject:[NSNumber numberWithInt:[[arrayForDistance objectAtIndex:i] intValue]] forKey:@"distance"];
        [self.arrayForRows addObject:tempDicts];
    }

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES];
    [self.arrayForRows sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
于 2013-06-12T09:47:09.450 回答