Detailed Explanation of IOS Development to Judge Whether the Data in Two Arrays Are the Same
- 2021-10-13 08:53:38
- OfStack
Detailed Explanation of IOS Development to Judge Whether the Data in Two Arrays Are the Same
Foreword:
The problems encountered in work, recorded here, may help everyone
Example code:
NSArray *array1 = [NSArray arrayWithObjects:@"a", @"b", @"c", nil nil];
NSArray *array2 = [NSArray arrayWithObjects:@"b", @"a", @"c", nil nil];
bool bol = false;
// Create two new arrays
NSMutableArray *oldArr = [NSMutableArray arrayWithArray:array1];
NSMutableArray *newArr = [NSMutableArray arrayWithArray:array2];
// Pair array 1 Sort.
[oldArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){
return obj1 > obj2;
}];
//// The previous sort doesn't seem to work, so the following one should be used
[oldArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){return [obj1 localizedStandardCompare: obj2];}];
// Pair array 2 Sort.
[newArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){
return obj1 > obj2;
}];
//// The previous sort doesn't seem to work, so the following one should be used
[newArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){return [obj1 localizedStandardCompare: obj2];}];
if (newArr.count == oldArr.count) {
bol = true;
for (int16_t i = 0; i < oldArr.count; i++) {
id c1 = [oldArr objectAtIndex:i];
id newc = [newArr objectAtIndex:i];
if (![newc isEqualToString:c1];) {
bol = false;
break;
}
}
}
if (bol) {
NSLog(@" The contents of both arrays are the same! ");
}
else {
NSLog(@" The contents of the two arrays are different! ");
}
Thank you for reading, hope to help everyone, thank you for your support to this site!