1

我被困在这里我在这里有纬度和经度数组,从 JSON Parsing 获取

Arr_Lat=[[[[main valueForKey:@"responseData"]valueForKey:@"results"]valueForKey:@"lat"] copy];
Arr_Long=[[[[main valueForKey:@"responseData"]valueForKey:@"results"]valueForKey:@"lng"] copy];

我已经计算了当前位置和经纬度索引数组之间的距离,并存储到一个数组中......

CLLocation *currLoc = [[CLLocation alloc] initWithLatitude:appDel.curr_lat longitude:appDel.curr_long];

CLLocation *destLoc = [[CLLocation alloc] initWithLatitude:appDel.str_lat longitude:appDel.str_long];

distance = [destLoc distanceFromLocation:currLoc]/1000;

DistStr = [[NSMutableString alloc]initWithFormat:@" %0.3f",distance];


    [appDel.Arr_distance addObject:DistStr];

我对距离进行了这样的排序

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


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

我已经显示了标题、地址和距离,但地址和距离不匹配我想根据升序距离数组对地址进行排序....

提前感谢您的帮助........

4

1 回答 1

0

Arr_distance 是 NSString ,我认为这样对数组进行排序是不正确的。试试这个:

sortedFloats = [appDel.Arr_distance sortedArrayUsingComparator:^(NSString *dis1, NSString *dis2){
    if ([dis1 floatValue] < [dis2 floatValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    } else {
        return (NSComparisonResult)NSOrderedAscending;
    }
}];]

您可能需要根据需要交换 NSOrderedDescending 或 NSOrderedAscending。

于 2012-12-06T08:19:09.650 回答