#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDictionary *[email protected]{@"name":@"小阳",
@"class":@"IOS8",
@"age":@"20"
};
NSDictionary *[email protected]{@"name":@"小红",
@"class":@"IOS8",
@"age":@"67"
};
NSDictionary *[email protected]{@"name":@"小丽",
@"class":@"IOS6",
@"age":@"12"
};
NSDictionary *[email protected]{@"name":@"小杨",
@"class":@"IOS8",
@"age":@"33"
};
NSDictionary *[email protected]{@"name":@"李俊威",
@"class":@"IOS6",
@"age":@"23"
};
NSDictionary *[email protected]{@"name":@"赵玉鑫",
@"class":@"IOS8",
@"age":@"9"
};
NSDictionary *[email protected]{@"name":@"胡旭阳",
@"class":@"IOS6",
@"age":@"20"
};
NSArray *arr=[[NSArray alloc]initWithObjects:dic1,dic2,dic3,dic4,dic5,dic6,dic7,nil];
//1.将以上四个字典存入数组中,输出数据以班级:XXX,姓名:XXX,年龄:XX格式。
NSLog(@"操作前数据????????????????????????");
for (NSDictionary *dic in arr) {
NSLog(@"班级:%@,姓名:%@,年龄:%@",dic[@"class"],dic[@"name"],dic[@"age"]);
}
//2.删除小阳此条数据,列出剩余数据,输出格式同上。
NSMutableArray *muarr=[NSMutableArray array];
for (int i=0; i<arr.count; i++) {
NSDictionary *dic=arr[i];
if ( ![ [dic objectForKey:@"name"] isEqual:@"小阳"]) {
[muarr addObject:dic];
}
}
NSLog(@"删除后数据????????????????????????");
for (NSDictionary *dic in muarr) {
NSLog(@"班级:%@,姓名:%@,年龄:%@",dic[@"class"],dic[@"name"],dic[@"age"]);
}
//3.按照班级进行排序,如果班级相同则按照年龄排序输出数据,格式同上。描述器
//NSSortDescriptor 指定用于对象数组排序的对象属性
NSSortDescriptor *classWithSort=[[NSSortDescriptor alloc]initWithKey:@"class" ascending:YES];
NSSortDescriptor *ageWithSort=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES];
NSArray *elementarr=[NSArray arrayWithObjects:ageWithSort,classWithSort, nil];
NSArray *sortArray=[arr sortedArrayUsingDescriptors:elementarr];
NSLog(@"排序后数据????????????????????????");
for (NSDictionary *dic in sortArray) {
NSLog(@"班级:%@,姓名:%@,年龄:%@",dic[@"class"],dic[@"name"],dic[@"age"]);
}
}
return 0;
}