2013-11-22 40 views
0

我需要使用其內部對象的屬性重新排序NSMutableArray。爲一個對象的雙重屬性排序數組

我在日期以前的項目就是這樣做的日期,​​但這種方法不接受雙打:

NSUInteger rowIndex = [self.alist indexOfObject:assignment inSortedRange:NSMakeRange(0, self.alist.count) options:NSBinarySearchingInsertionIndex usingComparator:^(AssignmentInfo *obj1, AssignmentInfo *obj2) { 

    NSDate *date1 = obj1.dateTime; 
    NSDate *date2 = obj2.dateTime; 

    if ([date1 compare:date2] == NSOrderedAscending) { 
     return NSOrderedAscending; 
    } 
    if ([date1 compare:date2] == NSOrderedDescending) { 
     return NSOrderedDescending; 
    } 
    return NSOrderedSame; 

}]; 

因此,沒有我需要的方式重新排列陣列self.communitiesArray爲社會目標的雙重價值在數組中。 self.community.distanceFromUser這是雙。

使用一些答案下面我就得到一個錯誤的:

NSUInteger rowIndex = [self.communitiesArray indexOfObject:community inSortedRange:NSMakeRange(0, self.communitiesArray.count) options:NSBinarySearchingInsertionIndex usingComparator:^(Community *obj1, Community *obj2) { 

錯誤說:不相容塊指針類型發送 '無效(^)(社區* _ 強,社區* _strong)'以類型的參數 'NSComparator'(又名 'NSComparisonResult(^)(__強大的ID,ID __strong)')

回答

3

您可以使用NSSortDescriptor對陣列進行排序。

[array sortUsingDescriptors:@[[[NSSortDescriptor alloc] initWithKey:@"community.distanceFromUser" ascending:YES]]]; 
0

只需直接比較double S,即,在比較:

double a = obj1.distanceFromUser; 
double b = obj2.distanceFromUser; 
if (a < b) { 
    return NSOrderedAscending; 
} else if (a > b) { 
    return NSOrderedDescending; 
} else { 
    return NSOrderedSame; 
} 
+0

我得到一個錯誤:'NSUInteger rowIndex = [self.communitiesArray indexOfObject:community inSortedRange:NSMakeRange(0,self.communitiesArray.count)options:NSB錯誤表示:不兼容的塊指針類型將'void(^)(Community * __ strong,Community * __ strong)'發送給'NSComparator'類型的參數(又名' NSComparisonResult(^)(__ strong id,__strong id)') –

3

由於double是原語,因此您不需要使用其中的compare:方法:將它們與常規運算符進行比較應該足夠了。你比較塊將如下所示:

double d1 = obj1.doubleValue; 
double d2 = obj2.doubleValue; 
if (d1 < d2) { 
    return NSOrderedAscending; 
} 
if (d1 > d2) { 
    return NSOrderedDescending; 
} 
return NSOrderedSame; 

一個更好的辦法是使用NSSortdescriptor S:他們讓你基於無需編寫任何額外的代碼,他們揭露性質的對象進行排序:

NSSortDescriptor *dblDescr= [[NSSortDescriptor alloc] initWithKey:@"doubleValue" ascending:YES]; 
NSArray *sortedArray = [myData sortedArrayUsingDescriptors:@[dblDescr]]; 
+0

我收到以下錯誤:'NSUInteger rowIndex = [self.communitiesArray indexOfObject:community inSortedRange:NSMakeRange(0,self.communitiesArray.count)options:NSBinarySearchingInsertionIndex usingComparator:^ (Community * obj1,Community * obj2){' 錯誤說:不兼容的指針類型發送'void(^)(Community * __ strong,Community * __ strong)'到'NSComparator'類型的參數(又名'NSComparisonResult(^)' (__strong id,__strong id)') –

+0

@SteveEdwin在dec中將'obj1'和'obj2'的類型改爲'id obj1,id obj2'並添加一個強制轉換到'Community *',或使用方括號語法'double d1 = [obj1 doubleValue]'而不是點語法'double d1 = obj1.doubleValue'。 – dasblinkenlight

0

正如有人說,double是基本類型,所以你可以使用比較操作符來比較它們

NSUInteger rowIndex = 
    [self.communitiesArray indexOfObject:community 
          inSortedRange:NSMakeRange(0, self.communitiesArray.count) 
            options:NSBinarySearchingInsertionIndex 
          usingComparator:^NSComparisonResult (Community *obj1, Community *obj2) { 
     double distance1 = obj1.distanceFromUser; 
     double distance2 = obj2.distanceFromUser; 
     if (distance1 < distance2) 
      return NSOrderedAscending; 
     if (distance1 < distance2) 
      return NSOrderedDescending; 
     return NSOrderedSame; 
} 

公告塊簽名:你路過^(Community *obj1, Community *obj2),這被解釋爲^void (Community *obj1, Community *obj2),而你必須包括正確的返回類型,即^NSComparisonResult (Community *obj1, Community *obj2)

+0

它實際上沒有排序?我看到這些值已經填滿了,但沒有任何反應? –

相關問題