私はNSFetchRequest
次の述語でを行っています:
[NSPredicate predicateWithFormat:@"index IN %@", indexes];
...ここindexes
で、NSOrderedSet
は数字です。
これにより、指定されたインデックスを持つ「ランダムにソートされた」オブジェクトの配列が得られます。オブジェクトが順序セットと同じ順序で表示されるように、これを並べ替える必要があります。
これを行うための最も効率的な方法は何でしょうか?
アップデート
マーティンRの回答に基づくカテゴリは次のとおりです。
@implementation NSArray (SortUsingValues)
- (NSArray *)sortedArrayUsingValues:(NSOrderedSet *)values keyPath:(NSString *)keyPath
{
NSAssert([values isKindOfClass:[NSOrderedSet class]], nil);
NSAssert([keyPath isKindOfClass:[NSString class]], nil);
return [self sortedArrayUsingComparator:^NSComparisonResult(id object, id otherObject) {
id value = [object valueForKeyPath:keyPath];
id otherValue = [otherObject valueForKeyPath:keyPath];
NSUInteger indexOfValue = [values indexOfObject:value];
NSUInteger indexOfOtherValue = [values indexOfObject:otherValue];
if (indexOfValue > indexOfOtherValue) return NSOrderedDescending;
else if (indexOfValue < indexOfOtherValue) return NSOrderedAscending;
else return NSOrderedSame;
}];
}
@end