0

範囲オブジェクトをNSMutable配列、つまり[{5、12} {0、4} {18、10}]に追加しました。次に、このNSMutable配列を昇順で並べ替えます。次のコードを使用して、NSMutable配列オブジェクトを昇順で取得しています。

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

しかし、それは必要な出力を与えていません。範囲オブジェクトを持つNSMutable配列をソートする方法を誰かが知っている場合。私を助けてください。

ありがとうございます。

4

1 に答える 1

5

配列に範囲を追加する場合は、使用します

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");
  } ];
于 2012-08-13T16:32:22.343 に答える