Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

The data needs to be able to sort based on ascending distance, obviously per userlocation What's the best way to approach this? I thought of picking apart all of the dictionaries and inserting the distance and then letting loose the NSSortDescriptor, problem is, i'm way too lazy for that.

Right now im just looping through all of the data with the indexPath.row and displaying it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
348 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...