添加,查找删除,首字母排序,分数大于80
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *arr=@[@{@"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数组内。
NSMutableArray *mutArr=[[NSMutableArray alloc]init];
[mutArr addObjectsFromArray:arr];
[mutArr addObject:@{@"name":@"Philip Schiller",@"age":@"29",@"sex":@"female",@"score":@"70"}];
NSLog(@"%@",mutArr);
NSLog(@"📚📚📚📚📚📚📚📚📚📚📚📚");
// 2.查找数组内"Steve Jobs"的数据并删除。
for (int i=0;i<mutArr.count;i++){
NSDictionary *dic=mutArr[i];
if ( [ [dic objectForKey:@"name"] isEqual:@"Steve Jobs"]) {
[mutArr removeObject:dic];
}
}
for(id sort in mutArr){
NSLog(@"name:%@ age:%@ sex:%@ score:%@ ",sort[@"name"],sort[@"age"],sort[@"sex"],sort[@"score"]);
}
NSLog(@"📚📚📚📚📚📚📚📚📚📚📚📚");
// 3.按姓名首字母进行排序。
NSSortDescriptor *sortArr=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:1];
NSArray *arr1=[mutArr sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortArr]];
for(NSDictionary *sort in arr1){
NSLog(@"%@ %@ %@ %@ ",sort[@"name"],sort[@"age"],sort[@"sex"],sort[@"score"]);
}
NSLog(@"📚📚📚📚📚📚📚📚📚📚📚📚");
// 4.按年龄进行升序排序,如果年龄相同则按性别进行排序。
NSSortDescriptor *ageWithSort=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:1];
NSSortDescriptor *sexWithSort=[[NSSortDescriptor alloc]initWithKey:@"sex" ascending:1];
NSArray *elementarr=[NSArray arrayWithObjects:ageWithSort,sexWithSort, nil];
NSArray *sortArray=[mutArr sortedArrayUsingDescriptors:elementarr];
for (NSDictionary *dic in sortArray) {
NSLog(@"%@ %@ %@ %@ ",dic[@"name"],dic[@"age"],dic[@"sex"],dic[@"score"]);
}
NSLog(@"📚📚📚📚📚📚📚📚📚📚📚📚");
// 5.输出成绩大于或等于80分的学员信息。
for(id sort in mutArr){
NSDictionary * xin=sort;
int num=[xin[@"score"] intValue];
if (num>=80) {
NSLog(@"%@ %@ %@ %@ ",xin[@"name"],xin[@"age"],xin[@"sex"],xin[@"score"]);
}
}
}
return 0;
}