gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:40:10 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com