iphone - 在 iPhone 上对 double 组或 CLLocationDistance 值进行排序

标签 iphone objective-c cocoa-touch arrays

我正在尝试按最近距离(升序)对 CLLocationDistance 值列表进行排序。我首先将值转换为 NSString 对象,以便我可以使用以下排序:

NSArray *sortedArray;
sortedArray = [distances sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

NSString 无法对数值进行排序。

如何按升序设置数值列表?

最佳答案

我想你想要的是 sortedArrayUsingFunction:context: 使用这样的东西:

NSInteger intSort(id num1, id num2, void *context)
{
    int v1 = [num1 intValue];
    int v2 = [num2 intValue];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

// sort things
NSArray *sortedArray; sortedArray = [anArray sortedArrayUsingFunction:intSort context:NULL];

apple docs reference

关于iphone - 在 iPhone 上对 double 组或 CLLocationDistance 值进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1422840/

相关文章:

iphone - MPMoviePlayerController-切换 View 时黑屏

iphone - 将 RestKit 与基于文件的 URL 结合使用

ios - 获取 PHAsset 的 url

iphone - 如何关闭UIWebView中的弹跳

ios - 隐藏/显示导航栏中两个右侧栏按钮之一

ios - Swift - 语音转文本并保存音频文件

iphone - 从照片中获取细节

objective-c - Xcode 4 项目中消失的变化

iphone - iOS - 如何有选择地删除文档目录中超过一个月的文件

ios - Amazon Web Service (AWS) S3,如何上传受密码保护的文件夹?