数组字典 添加 查找删除 按名字排序 安年龄排序 输出大于80分的学生

#import <Foundation/Foundation.h>

#define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        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数组内。

        NSMutableArray *arr=[NSMutableArray arrayWithArray:arr3];

        [arr addObject:@{@"name":@"Philip Schiller",@"age":@"29",@"sex":@"female",@"score":@"70"}];

        for(id ser in arr){

            NSLog(@"姓名%@ 年龄%@ 性别%@ 分数%@",ser[@"name"],ser[@"age"],ser[@"sex"],ser[@"score"]);

        }

//         2.查找数组内"Steve Jobs"的数据并删除。

        NSLog(@"2");

        for (int i=0; i<arr.count; i++) {

            NSDictionary *dic1=arr[i];

            if ([dic1[@"name"] isEqual:@"Steve Jobs"]) {

                [arr removeObject:dic1];

            }

        }

        for(id ser in arr){

            NSLog(@"姓名%@ 年龄%@ 性别%@ 分数%@",ser[@"name"],ser[@"age"],ser[@"sex"],ser[@"score"]);

        }

        //         3.按姓名首字母进行排序。

         NSLog(@"3");

        NSSortDescriptor *NameWithsort=[[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];

        NSArray *weer=[NSArray arrayWithObjects:NameWithsort, nil];        

        NSArray *weer1=[arr sortedArrayUsingDescriptors:weer];

        for(id ser in weer1){

            NSLog(@"姓名%@ 年龄%@ 性别%@ 分数%@",ser[@"name"],ser[@"age"],ser[@"sex"],ser[@"score"]);

        }

//         4.按年龄进行升序排序,如果年龄相同则按性别进行排序。

        NSLog(@"4");

        NSSortDescriptor *AgeWithsort=[[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES];

        NSArray *weer2=[NSArray arrayWithObjects:AgeWithsort, nil];   

        NSArray *weer3=[arr sortedArrayUsingDescriptors:weer2];

        for(id ser in weer3){

            NSLog(@"姓名%@ 年龄%@ 性别%@ 分数%@",ser[@"name"],ser[@"age"],ser[@"sex"],ser[@"score"]);

        }

//         5.输出成绩大于或等于80分的学员信息。

        NSLog(@"55555555555555555555555");

        NSSortDescriptor *ScoreWithsort=[[NSSortDescriptor alloc]initWithKey:@"score" ascending:YES];

        NSArray *weer4=[NSArray arrayWithObjects:ScoreWithsort, nil];

        NSArray *weer5=[arr sortedArrayUsingDescriptors:weer4];

        for(NSDictionary *ser in weer5){

            NSInteger num=[ser[@"score"] integerValue];    

            if (num>=80) {

                NSLog(@"姓名%@ 年龄%@ 性别%@ 分数%@",ser[@"name"],ser[@"age"],ser[@"sex"],ser[@"score"]);

            }}

    }

    return 0;

}

原文地址:https://www.cnblogs.com/j-h-t-123-n/p/5121394.html