Sort by distance

后端 未结 1 497
时光说笑
时光说笑 2020-12-03 02:38

I have a set of dictionaries in an array. I display this data in a tableview. In the tableview i calculate the distance to each object and display that in the cell.

相关标签:
1条回答
  • 2020-12-03 03:03

    You can easily sort with a the block based comparator api.

    NSArray *rawData = [NSArray arrayWithContentsOfFile:pathDoc];
    
    rawData = [rawData sortedArrayUsingComparator: ^(id a, id b) {
    
        CLLocationDistance dist_a= [[a objectsForKey:@"coordinate"] distanceFromLocation: userPosition];
        CLLocationDistance dist_b= [[b objectsForKey:@"coordinate"] distanceFromLocation: userPosition];
        if ( dist_a < dist_b ) {
            return NSOrderedAscending;
        } else if ( dist_a > dist_b) {
            return NSOrderedDescending;
        } else {
            return NSOrderedSame;
        }
    }];
    

    You should easy find an implementation for calculate_distance(user_position, a); with google.

    0 讨论(0)
提交回复
热议问题