objective-c - 按距离排序

标签 objective-c ios cocoa-touch

我在数组中有一组字典。我在表格 View 中显示此数据。在表格 View 中,我计算到每个对象的距离并将其显示在单元格中。

数据需要能够根据升序排序,显然是每个用户位置 什么是解决这个问题的最佳方法?我想分开所有的字典并插入距离,然后放开 NSSortDescriptor,问题是,我太懒了。

现在我只是使用 indexPath.row 遍历所有数据并显示它。

最佳答案

您可以使用基于 block 的比较器 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 的实现。

关于objective-c - 按距离排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7769492/

相关文章:

ios - Objective-C : validity check for an enum variable

ios - 从 xib 文件中以模态方式呈现 View Controller

objective-c - 自定义 UISlider - 增加 "hot spot"大小

iphone - 在被遮挡时激活接近监控以关闭 iPhone 屏幕

ios - 使用自定义 UITableViewCell 时,删除确认按钮不会消失

ios - 获取 MKMapView 以响应 touch up inside 事件

iphone - block 而不是 performSelector :withObject:afterDelay:

iPhone willResignActive 方法未完成执行

ios - 代码从swift 2.0迁移到swift 3.0后出现错误

ios - 当我在设备中运行我的应用程序时出现 dyld`gdb_image_notifier 异常