我正在尝试按最近距离(升序)对CLLocationDistance值列表进行排序。我首先将值转换为NSString对象,以便可以使用以下排序:
NSArray *sortedArray;
sortedArray = [distances sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];NSString无法对数值进行排序。
如何按升序设置数值列表?
发布于 2009-09-14 17:22:53
我认为您想要的是使用如下内容的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
发布于 2009-09-14 17:21:23
尝试在定义函数的地方使用sortedArrayUsingSelector来比较元素,这里有一个引用:http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/sortedArrayUsingSelector
发布于 2009-09-14 17:24:00
当您将值转换为字符串时,排序将是字典排序,而不是数字排序,根据您的问题,这不是您想要的。根据苹果文档,CLLocationDistance被定义为double类型。鉴于此,使用用CLLocationDistance数据初始化的NSNumber实例创建NSArray (请参见numberWithDouble),并对这些实例使用NSArray的排序实现。
有关NSNumber的更多信息,请访问here。
https://stackoverflow.com/questions/1422840
复制相似问题