在 iPhone 上对双精度数组或 CLLocationDistance 值进行排序 [英] Sorting an array of doubles or CLLocationDistance values on the iPhone

查看:19
本文介绍了在 iPhone 上对双精度数组或 CLLocationDistance 值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to sort a list of CLLocationDistance values by closest distance (ascending order). I first converted the values to NSString objects so that I could use the following sort:

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

NSString 不能对数值进行排序.

NSString cannot sort on the numeric value.

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

How can I set a list of numeric values in ascending order?

推荐答案

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

I think what you want is sortedArrayUsingFunction:context: using something like this:

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

苹果文档参考

这篇关于在 iPhone 上对双精度数组或 CLLocationDistance 值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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