排序双打或CLLocationDistance值的阵列上的iPhone [英] Sorting an array of doubles or CLLocationDistance values on the iPhone
问题描述
我想排序最近距离(升序) 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:背景:
使用这样的:
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];
<一个href=\"http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray%5FClass/NSArray.html#//apple%5Fref/occ/instm/NSArray/sortedArrayUsingFunction%3Acontext%3A\">apple文档引用
这篇关于排序双打或CLLocationDistance值的阵列上的iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!