关于数组Array遍历,比较排序的一些基本方法使用

0 用户选择

   //把数据源拿出来创建临时的数组,不要直接使用数据源
NSArray *answer = @[@1, @2, @3, @5, @6, @7, @8, @10];//答案数组
NSArray *select = @[@1, @4, @3, @9];    //用户选的选项

if ([answer isEqualToArray:select]) {
    //一样就是对的
    
}else {
    //不一样就是错的
    //拿出来answer 和 select 中一样的
    NSArray *selectTure = [answer filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@", select]];
    NSLog(@"用户选择对的 ->(即取出相同的元素:) %@", selectTure);
    
    NSArray *selectWrong = [select filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (SELF in %@)", answer]];
    NSLog(@"用户选择是错的 -> %@", selectWrong);
    
    NSArray *unselectTure = [answer filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (SELF in %@)", selectTure]];
    NSLog(@"用户没选择的正确答案 -> %@", unselectTure);  
    
}

1 去除array内重复的 对象

//去除实体内 name 重复的对象
 NSMutableDictionary *dic = [NSMutableDictionary dictionary];
for (int i = 0; i <arrList_.count ; i++) {
    Entity *entity1 = arrList_[i];
    [dic setValue:entity1 forKey:entity1.name];
}
NSArray *tListEntity = [dic allValues];
[arrList_ removeAllObjects];
arrList_ = [NSMutableArray arrayWithArray:tListEntity];

2 取出array内相同的 对象

    NSMutableArray *rescanList = [NSMutableArray array];
[arrList_ enumerateObjectsUsingBlock:^(Entity *entity1, NSUInteger idx1, BOOL *stop1) {
    [pList enumerateObjectsUsingBlock:^(Entity *entity2, NSUInteger idx2, BOOL *stop2) {
        if ([entity2.name isEqualToString:entity1.name]) {
            [rescanList addObject:entity2.name];
        }
    }];
}];

3 array按照对象某个属性的 字母排序 文字 字母排序

    if (pListInventoryWaveArr.count > 0) {
    pListInventoryWaveArr = [[NSMutableArray alloc] initWithArray:[pListInventoryWaveArr sortedArrayUsingComparator:^NSComparisonResult(Entity * obj1, Entity * obj2) {
        return [obj1.numbmer compare:obj2.numbmer options:NSNumericSearch];
    }]];
}

4 array 根据返回时间排序

引自: NSArray和NSMutableArray内容排序(如按日期排序)

 NSMutableArray *feedsBuffer;
//初始化buffer以及填充数据
//排序只需要两句话:已针对数组内对象的publishTime属性(NSDate)排序为例:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"publishTime" ascending:NO];
[feedsBuffer sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

假如feedsBuffer是NSArray,那么有一个方法是

NSArray *sortedArray = [feedsBuffer sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

如果排序的依据比较复杂,那么可以使用Block进行大小判断自定义:

[feedsBuffer sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//返回三者其一:NSOrderedAscending, NSOrderedSame, NSOrderedDescending
return NSOrderedSame;
}];

正则表达式-->可应用于对象直接进行比较,他会逐一遍历数组内的对象内的每一个属性

 // 1 查找相同的数据
NSArray * arr2 = @[@4,@3,@2,@1];
NSArray * arr1 = @[@2,@3,@4,@5];
NSPredicate * filterPredicate_same = [NSPredicate predicateWithFormat:@"SELF IN %@",arr1];
NSArray * filter_no = [arr2 filteredArrayUsingPredicate:filterPredicate_same];
NSLog(@"%@",filterPredicate_same);

-----------------------华丽的分割线--------------

// 查找不同的数据
NSArray * arr2 = @[@4,@3,@2,@1];
NSArray * arr1 = @[@2,@3,@4,@5];
//找到在arr2中不在数组arr1中的数据
NSPredicate * filterPredicate1 = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",arr1];
NSArray * filter1 = [arr2 filteredArrayUsingPredicate:filterPredicate1];
//找到在arr1中不在数组arr2中的数据
NSPredicate * filterPredicate2 = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",arr2];
NSArray * filter2 = [arr1 filteredArrayUsingPredicate:filterPredicate2];
//NSArray * filter2 = [arr1 filteredArrayUsingPredicate:filterPredicate2];
//拼接数组
NSMutableArray *array = [NSMutableArray arrayWithArray:filter1];
[array addObjectsFromArray:filter2];
NSLog(@"%@",array);

推荐阅读更多精彩内容