#define NSLog(FORMAT, ...) fprintf(stderr,"%s\n",[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#import <Foundation/Foundation.h>
static NSString * const kName = @"name";
static NSString * const kAge = @"age";
static NSString * const kGender = @"gender";
inline static NSMutableDictionary *CreateDict(NSString *name, NSInteger age, BOOL isMale){
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:@{kName:name,kAge :@(age),kGender :@(isMale)}];
return dict;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *arr = @[CreateDict(@"王思聪", 27, YES),
CreateDict(@"范冰冰", 35, NO),
CreateDict(@"马云", 51, YES),
CreateDict(@"霍建华", 36, YES),
CreateDict(@"赵丽颖", 28, NO)];
//NSMutableArray *arr=[NSMutableArray array];
NSSortDescriptor *sortByAge=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES];//按年龄升序排序
NSArray *elementarrr=[NSArray arrayWithObjects:sortByAge, nil];//分配排序的优先顺序
NSArray *sortArray=[arr sortedArrayUsingDescriptors:elementarrr];//然后按照排好序的数组中的顺序进行排序,到此处已经排好
for(NSDictionary *dic in sortArray )
{
if([[dic objectForKey:kGender] isEqual:@NO])
{
NSLog(@"%@,今年%@岁",dic[kName],dic[kAge]);
}else{
NSLog(@"%@,今年%@岁",dic[kName],dic[kAge]);
}
}
NSLog(@"\n");
//2//
NSSortDescriptor *sortByAges=[[NSSortDescriptor alloc]initWithKey:kAge ascending:YES];
NSArray *elementarrrs=[NSArray arrayWithObjects:sortByAges, nil];
NSArray *sortArrays=[arr sortedArrayUsingDescriptors:elementarrrs];
for(NSDictionary *dic in sortArrays )
{
BOOL boo=[[dic objectForKey:kGender] boolValue];
if(boo==0)
{
NSLog(@"%@大美女,今年%@岁",dic[kName],dic[kAge]);
}
}
NSLog(@"\n");
//3//
for(NSDictionary *dic6 in arr)
{
if([dic6[kName] isEqualToString:@"马云"])
{
[dic6 setValue:@"41" forKeyPath:kAge];
}
}
for(NSDictionary *dic7 in arr)
{
BOOL boo=[[dic7 objectForKey:kGender] boolValue];
if(boo==1){
NSLog(@"姓名:%@,年龄:%@,男性",dic7[kName],dic7[kAge]);
}
}
}
//更改马云的年龄
for(NSDictionary *dic6 in arr)
{
if([dic6[kName] isEqualToString:@"马云"])
{
[dic6 setValue:@"41" forKeyPath:kAge];
}
}
for(NSDictionary *dic7 in arr)
{
BOOL boo=[[dic7 objectForKey:kGender] boolValue];
if(boo==1){
NSLog(@"姓名:%@,年龄:%@,男性",dic7[kName],dic7[kAge]);
}
}
return 0;
}