【问题标题】:Sort an array for an objects double property对对象双属性的数组进行排序
【发布时间】:2013-11-22 02:56:08
【问题描述】:

我需要使用 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) {

错误提示:不兼容的块指针类型将“void (^)(Community *_strong, Community *_strong)”发送到“NSComparator”类型的参数(又名“NSComparisonResult (^)(__strong id, __strong id)')

【问题讨论】:

    标签: ios objective-c arrays sorting


    【解决方案1】:

    您可以使用NSSortDescriptor 对数组进行排序。

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

    【讨论】:

      【解决方案2】:

      由于doubles 是原语,您不需要对它们使用compare: 方法:将它们与常规运算符进行比较就足够了。您的比较器块如下所示:

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

      更好的方法是使用NSSortdescriptors:它们允许您根据对象公开的属性对对象进行排序,而无需编写任何额外的代码:

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

      【讨论】:

      • 我收到一个错误: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)')
      • @SteveEdwin 在声明中将obj1obj2 的类型更改为id obj1, id obj2,并为Community* 添加强制转换,或者使用方括号语法double d1=[obj1 doubleValue] 而不是点语法double d1 = obj1.doubleValue.
      【解决方案3】:

      只需直接比较doubles,即在比较器中:

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

      【讨论】:

      • 我收到一个错误: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)')
      【解决方案4】:

      正如其他人所说,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)

      【讨论】:

      • 实际上没有排序?我看到值被填满但没有任何反应?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2012-02-20
      • 2010-11-02
      • 1970-01-01
      相关资源
      最近更新 更多