[英]How to sort an NSmutable array with ascending order of distance?
I have an nsmutable array of friends list.each having their lat&longs.I want to sort that array with the ascending rate of their distances from the autor.I found out the distances values of the friends with the autor,Now I want to sort that array in the ascending of their distances.This is how i am doing that,` 我有一个nsmutable朋友列表list.each有他们的lat和longs.I想要按照他们与autor的距离的上升速率对该数组进行排序。我发现了与autor的朋友的距离值,现在我想要排序在他们的距离上升的数组。这就是我这样做的方式,`
for(int i=0;i<[searchfriendarray count];i++)
{
NSDictionary *payload =[searchfriendarray objectAtIndex:i];
NSLog(@"%@",payload);
NSString *memberid = [payload objectForKey:@"userID"];
CLLocation *locationofauthor;
CLLocation *locationoffriends;
if([memberid isEqualToString:uidstr])
{
NSString *latofauthor = [payload objectForKey:@"latitude"];
NSString *longofauthor=[payload objectForKey:@"longitude"];
double latofauthordouble = [latofauthor doubleValue];
double longofauthordouble=[longofauthor doubleValue];;
locationofauthor = [[CLLocation alloc] initWithLatitude:latofauthordouble longitude:longofauthordouble];
}
else
{
NSString *latoffriends = [payload objectForKey:@"latitude"];
NSString *longoffriends=[payload objectForKey:@"longitude"];
double latoffriendsdouble = [latoffriends doubleValue];
double longoffriendsdouble=[longoffriends doubleValue];;
locationoffriends = [[CLLocation alloc] initWithLatitude:latoffriendsdouble longitude:longoffriendsdouble];
}
CLLocationDistance distance = [locationofauthor distanceFromLocation:locationoffriends];
}
`Can any body help me to sort my array in the ascecending order of the distances? “任何身体都可以帮助我按距离的升序排序我的阵列吗?
You can supply a block of comparison code between 2 objects. 您可以在两个对象之间提供一组比较代码。 NSArray will then call your block as many times as it needs to sort the array. 然后,NSArray将根据需要对块进行多次调用来调用块。
NSArray sortedArray = [yourUnsortedArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
/*some code to compare obj1 to obj2.
for instance, compare the distances of obj1 to obj2.
Then return an NSComparisonResult
(NSOrderedAscending, NSOrderedSame, NSOrderedDescending);*/
}];
ps to get a mutable array again, just call mutableCopy
on the returned object. ps再次获取一个可变数组,只需在返回的对象上调用mutableCopy
。
NSSortDescriptor * descLastname = [[NSSortDescriptor alloc] initWithKey:@"active" ascending:YES];
[livevideoparsingarray sortUsingDescriptors:[NSArray arrayWithObjects:descLastname, nil]];
[descLastname release];
videoparsing = [livevideoparsingarray copy];
livevideoparsingarray is my array which I have sort & active is my tag which is in array which I have sort. livevideoparsingarray是我的数组,我有排序和活动是我的标签,这是我排序的数组。 You change with your requirements. 你根据自己的要求改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.