简体   繁体   中英

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,`

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 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.

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. You change with your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM