I have two NSArrays, each containing NSStrings.
How do I check they are equivalent, ignoring case and order?
I've been playing around with NSSets and NSPredicates, but not having much luck.
I have two NSArrays, each containing NSStrings.
How do I check they are equivalent, ignoring case and order?
I've been playing around with NSSets and NSPredicates, but not having much luck.
Simple (case insensitive)
NSSet* one = [[NSCountedSet setWithArray:firstArray] valueForKey:@"lowercaseString"];
NSSet* two = [[NSCountedSet setWithArray:secondArray] valueForKey:@"lowercaseString"];
return [one isEqualToSet:two];
Complex (case and diacritic insensitive)
This solution is more complex, because it requires a special string comparison. You can either accomplish this by iterating sorted arrays using this method, or, if you wish to use valueForKey:
, using the following code.
@interface LNComparisonHelper : NSObject
- (instancetype)initWithString:(NSString*)str;
@property (nonatomic, copy, readonly) string;
@end
@implementation LNComparisonHelper
- (instancetype)initWithString:(NSString*)str
{
self = [super init];
if(self) { _str = [str copy]; }
return self;
}
- (BOOL)isEqual:(LNComparisonHelper*)other
{
return [_str compare:other.string options:(DiacriticInsensitiveSearch | CaseInsensitiveSearch)] == OrderedSame;
}
@end
@interface NSString (ComparisonWrapper)
- (id) ln_casediacriticInsensitiveComparator;
@end
@implementation NSString (ComparisonWrapper)
- (id) ln_casediacriticInsensitiveComparator
{
return [[LNComparisonHelper alloc] initWithString:self];
}
@end
NSSet* one = [[NSCountedSet setWithArray:firstArray] valueForKey:@"ln_casediacriticInsensitiveComparator"];
NSSet* two = [[NSCountedSet setWithArray:secondArray] valueForKey:@"ln_casediacriticInsensitiveComparator"];
return [one isEqualToSet:two];
As soon as there is a failure return NO
If no failure YES
Note: For reasonably sized arrays sorting has a low cost close to O(n).