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

2018-05-08  本文已影响69人  幸福的尾巴__

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 根据返回时间排序

    [array sortUsingSelector:@selector(compareNsdate)]

- (NSComparisonResult)compareNsdate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    NSMutableArray *tempArray = [NSMutableArray array];
       // 这里遍历的是一个对象的属性  即可以排序 与3的使用基本相同
    for (NSString *dateString in dateArray) {
        NSDate *date = [formatter dateFromString:dateString];
        [tempArray addObject:date];
    }
    [tempArray sortUsingComparator:^NSComparisonResult(NSDate *date1, NSDate *date2) {
        
        return [date2 compare:date1];
    }];
}

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

 // 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);
上一篇下一篇

猜你喜欢

热点阅读