在iPhone上对双精度数组或CLLocationDistance值进行排序

Atm*_*tma 4 arrays iphone cocoa-touch objective-c

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

NSArray *sortedArray;
sortedArray = [distances sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
Run Code Online (Sandbox Code Playgroud)

NSString无法对数值进行排序.

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

slf*_*slf 8

我想你想要的是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];
Run Code Online (Sandbox Code Playgroud)

apple docs参考