简体   繁体   English

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

[英]sort NSMutablearray having range objects

I have add the range objects to NSMutable array ie [{5, 12} {0, 4} {18, 10}]. 我已经将范围对象添加到NSMutable数组,即[{5,12} {0,4} {18,10}]。 Now I want to sort this NSMutable array in increasing order. 现在,我想以递增顺序对这个NSMutable数组进行排序。 I am using the following code to get the NSMutable array objects in increasing order. 我正在使用以下代码以递增顺序获取NSMutable数组对象。

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

But it is not giving the required output. 但是它没有提供所需的输出。 If anyone know that how to sort the NSMutable array having range objects. 如果有人知道如何对具有范围对象的NSMutable数组进行排序。 Please help me out. 请帮帮我。

Thanks to all. 谢谢大家。

If you want to add ranges to an array, use 如果要向数组添加范围,请使用

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

When you want to sort the array: 当您要对数组排序时:

[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