iphone - 对具有范围对象的 NSMutablearray 进行排序

标签 iphone objective-c ios

我已将范围对象添加到 NSMutable 数组,即 [{5, 12} {0, 4} {18, 10}]。现在我想按递增顺序对这个 NSMutable 数组进行排序。我正在使用以下代码以递增顺序获取 NSMutable 数组对象。

        NSArray *stringIndexes = [StringIndexes copy];
        stringIndexes = [stringIndexes sortedArrayUsingSelector:@selector(compare:)];
        NSLog(@"stringIndexes..:%@",stringIndexes);

但它没有提供所需的输出。如果有人知道如何对具有范围对象的 NSMutable 数组进行排序。请帮帮我。

感谢大家。

最佳答案

如果要将范围添加到数组,请使用

NSValue *value = [NSValue valueWithRange:(NSRange)range];

当你想对数组进行排序时:

[myArray sortUsingComparator:^NSComparisonResult(NSValue *val1, NSValue *val2, BOOL *stop) {
  NSRange range1 = [val1 rangeValue];
  NSRange range2 = [val2 rangeValue];
  // you need to fine tune the test below - not sure what you want
  if(range1.location == range2.location) return NSOrderedSame;
  if(range1.location < range2.location) return NSOrderedAscending;
  if(range1.location > range2.location) return NSOrderedDescending;
  assert(!"Impossible");
  } ];

关于iphone - 对具有范围对象的 NSMutablearray 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11935791/

相关文章:

iphone - 我仍如何播放iPhone(ipod touch)被锁定但程序正在运行的声音?

objective-c - 在 iPhone 上使用 FFMPeg 解码 WMA

ios - 异步请求图片后获取图片大小

ios - countDownTimer = 0 的 UIDatePicker 显示 "0 hours 1 min"

java - iphone - 检查 APNS 证书的有效性和过期时间

ios - 如何让我的应用程序显示为发送屏幕截图的选项?

objective-c - 如何让 UILabel 文本在几秒后消失?

ios - 具有通用布局的 Xcode 6 ScrollView

ios - 了解 numberForPlot : and numberOfRecordsForPlot: Core Plot

iphone - 使用 iPhone 加速度计沿 x 轴移动 Sprite