当前位置:  开发笔记 > 后端 > 正文

如何使用sortUsingFunction:context

如何解决《如何使用sortUsingFunction:context》经验,为你挑选了1个好方法。



1> Vassily..:

这里有一些可能对你有帮助的代码,(我花了很多时间让它工作,doc不是很有用)

它计算POI对象和currentLocation之间的距离,并从该位置开始最接近最远的POI对象

NSInteger compareDistance(id num1, id num2, void *context) 
{

int retour;
// fist we need to cast all the parameters
CLLocation* location = context;
POI* param1 = num1;
POI* param2 = num2;

// then we can use them as standard ObjC objects
CLLocation* locationCoordinates = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
CLLocation* locationPOI1 = [[CLLocation alloc] initWithLatitude:param1.coords.latitude longitude:param1.coords.longitude];
CLLocation* locationPOI2 = [[CLLocation alloc] initWithLatitude:param2.coords.latitude longitude:param2.coords.longitude];
CLLocationDistance distance1 = [locationPOI1 distanceFromLocation:locationCoordinates];
CLLocationDistance distance2 = [locationPOI2 distanceFromLocation:locationCoordinates];

//make the comparaison
if (distance1 < distance2)
    retour = NSOrderedAscending;
else if (distance1 > distance2)
    retour = NSOrderedDescending;
else
    retour = NSOrderedSame;

[locationCoordinates release];
[locationPOI1 release];
[locationPOI2 release];
return retour;

 }

 -(void) orderByProximityFromLocation:(CLLocationCoordinate2D) coordinates
 {
CLLocation* currentLocation = [[CLLocation alloc] initWithLatitude:coordinates.latitude longitude:coordinates.longitude];
[listOfPOI sortUsingFunction:compareDistance context:currentLocation];
[currentLocation release];

 }

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有