按距离排序 [英] Sort by distance

查看:111
本文介绍了按距离排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数组中有一组字典。我在tableview中显示这些数据。在tableview中,我计算到每个对象的距离,并在单元格中显示。

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.

数据需要能够根据上升的距离进行排序,显然是每个用户分配什么是最好的方法?我想到拾取所有的字典和插入距离,然后让松散的NSSortDescriptor,问题是,我的方式太懒了。

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.

现在,只需使用indexPath.row循环遍历所有数据并显示它。

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

推荐答案

您可以使用基于块的比较器api轻松排序。

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;
    }
}];

您应该很容易找到 calculate_distance(user_position,a); 与google。

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

这篇关于按距离排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆