0

我有一个 nsmutable 朋友列表数组。每个人都有他们的 lat&longs。我想用他们与 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];

}

`任何人都可以帮助我按照距离的升序对数组进行排序吗?

4

2 回答 2

2

您可以提供 2 个对象之间的比较代码块。然后 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 再次获取一个可变数组,只需调用mutableCopy返回的对象。

于 2013-02-02T10:24:16.393 回答
0
NSSortDescriptor * descLastname = [[NSSortDescriptor alloc] initWithKey:@"active" ascending:YES];
[livevideoparsingarray sortUsingDescriptors:[NSArray arrayWithObjects:descLastname, nil]];
 [descLastname release];
videoparsing = [livevideoparsingarray copy];

livevideoparsingarray 是我的数组,我有排序 & active 是我的标签,它在我有排序的数组中。你随着你的要求而改变。

于 2013-02-02T15:59:46.450 回答