我已将范围对象添加到 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/