2013-11-22 41 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

它实际上没有排序?我看到这些值已经填满了,但没有任何反应? –

相关问题