簡體   English   中英

對具有范圍對象的NSMutablearray進行排序

[英]sort NSMutablearray having range objects

我已經將范圍對象添加到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");
  } ];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM