2013-02-02 54 views
0

我有一個nsmutable數組的朋友list.each有他們的經緯度& longs.I想排序與他們距離autor.I的距離的上升速度數組,我發現了朋友與作者的距離值,現在我要排序他們distances.This的上升數組如何我做的是,'如何按距離升序排序一個NSmutable數組?

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]; 

} 

`任何機構可以幫我把我的距離的ascecending順序排列排序?

+0

你是從CoreData獲取這個數組嗎? –

+0

否....從服務中返回? – hacker

+0

你的主要問題是什麼?你在CLLocationDistance距離中獲得所有的距離。將其存儲在數組中。這是雙重價值。你不知道如何分類雙重價值? – CRDave

回答

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);*/ 
    }]; 

p.s.要再次獲得可變數組,只需在返回的對象上調用mutableCopy即可。

0
NSSortDescriptor * descLastname = [[NSSortDescriptor alloc] initWithKey:@"active" ascending:YES]; 
[livevideoparsingarray sortUsingDescriptors:[NSArray arrayWithObjects:descLastname, nil]]; 
[descLastname release]; 
videoparsing = [livevideoparsingarray copy]; 

livevideoparsingarray是我的數組,我有那種&活躍的是我的標籤,它是數組,我有排序。你改變你的要求。