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

Ket*_*nde 0 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);
Run Code Online (Sandbox Code Playgroud)

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

谢谢大家.

Dav*_*d H 5

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

NSValue *value = [NSValue valueWithRange:(NSRange)range];
Run Code Online (Sandbox Code Playgroud)

如果要对数组进行排序:

[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");
  } ];
Run Code Online (Sandbox Code Playgroud)