IOS开发之判断两个数组中数据是否相同实例详解

时间:2022-05-09 06:54:20

IOS开发之判断两个数组数据是否相同实例详解

前言:

工作中遇到的问题,这里记录下,也许能帮助到大家

实例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
NSArray *array1 = [NSArray arrayWithObjects:@"a", @"b", @"c", nil nil];
NSArray *array2 = [NSArray arrayWithObjects:@"b", @"a", @"c", nil nil];
bool bol = false;
 
//创建俩新的数组
NSMutableArray *oldArr = [NSMutableArray arrayWithArray:array1];
NSMutableArray *newArr = [NSMutableArray arrayWithArray:array2];
 
//对数组1排序。
[oldArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){
  return obj1 > obj2;
}];
 
////上个排序好像不起作用,应采用下面这个
[oldArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){return [obj1 localizedStandardCompare: obj2];}];
 
//对数组2排序。
[newArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2){
   return obj1 > obj2;
   }];
////上个排序好像不起作用,应采用下面这个
[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(@"两个数组的内容相同!"); 
else
  NSLog(@"两个数组的内容不相同!"); 
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!