OC 之字典/数组混合使用(删除元素 描述器排序)
注:字典在数组内排序用描述器排序,遍历用字典对象输出!!!!!!
NSArray *arr3=@[@{@"name":@"Tim Cook",@"age":@"24",@"sex":@"female",@"score":@"89"},@{@"name":@"Jony Ive",@"age":@"26",@"sex":@"female",@"score":@"76"},@{@"name":@"Steve Jobs",@"age":@"24",@"sex":@"male",@"score":@"67"},@{@"name":@"Robert Brunne",@"age":@"28",@"sex":@"male",@"score":@"88"}]; /* 1.添加数据姓名:Philip Schiller年龄:29性别:female分数:70到arr数组内。 2.查找数组内"Steve Jobs"的数据并删除。 3.按姓名首字母进行排序。 4.按年龄进行升序排序,如果年龄相同则按性别进行排序。 5.输出成绩大于或等于80分的学员信息。*/ NSDictionary *dic=@{@"name":@"Philip Schiller", @"age":@"29", @"sex":@"female", @"score":@"70"}; NSArray *arr4=[arr3 arrayByAddingObject:dic]; NSLog(@"姜易成"); NSLog(@"第一题------------"); for(NSDictionary *dic in arr4){ NSLog(@"%@ %@ %@ %@",dic[@"name"],dic[@"age"],dic[@"sex"],dic[@"score"]); } NSMutableArray *arr5=[NSMutableArray arrayWithArray:arr4]; NSMutableArray *arr6=[NSMutableArray arrayWithArray:arr4]; NSLog(@"第二题------------"); for(int i=0;i<arr4.count;i++){ NSDictionary *dic=arr4[i]; if([dic[@"name"] isEqual:@"Steve Jobs"]){ [arr5 removeObject:dic]; } } for(NSDictionary *dic in arr5){ NSLog(@"%@ %@ %@ %@",dic[@"name"],dic[@"age"],dic[@"sex"],dic[@"score"]); } NSSortDescriptor *sorts=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; [arr6 sortUsingDescriptors:[NSArray arrayWithObjects:sorts, nil]]; NSLog(@"第三题------------"); for(NSDictionary *dic in arr6){ NSLog(@"%@ %@ %@ %@",dic[@"name"],dic[@"age"],dic[@"sex"],dic[@"score"]); } NSLog(@"第四题------------"); NSSortDescriptor *sortAge=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]; NSSortDescriptor *sortSex=[NSSortDescriptor sortDescriptorWithKey:@"sex" ascending:YES]; [arr6 sortUsingDescriptors:[NSArray arrayWithObjects:sortAge,sortSex, nil]]; for(NSDictionary *dic in arr6){ NSLog(@"%@ %@ %@ %@",dic[@"name"],dic[@"age"],dic[@"sex"],dic[@"score"]); } NSLog(@"第五题------------"); for(NSDictionary *dic in arr6){ int score =[dic[@"score"] intValue]; if(score>80){ NSLog(@"%@ %@ %@ %@",dic[@"name"],dic[@"age"],dic[@"sex"],dic[@"score"]); } }