美文网首页iOS Developer
IOS 字典与数组的排序

IOS 字典与数组的排序

作者: JakieZhang | 来源:发表于2016-12-03 10:19 被阅读38次

    TRStudent*stu1 = [TRStudentstudentWithName:@"张三"andAge:18];

    TRStudent*stu2 = [TRStudentstudentWithName:@"李四"andAge:20];

    TRStudent*stu3 = [TRStudentstudentWithName:@"王五"andAge:19];

    TRStudent*stu4 = [TRStudentstudentWithName:@"赵六"andAge:22];

    TRStudent*stu5 = [TRStudentstudentWithName:@"钱七"andAge:21];

    NSArray*stu =@[stu1,stu2,stu3,stu4,stu5];

    NSArray*sorted = [stusortedArrayUsingComparator:^NSComparisonResult(idobj1,idobj2) {

    NSNumber*obj1Age = [NSNumbernumberWithInt:[obj1age]];

    NSNumber*obj2Age = [NSNumbernumberWithInt:[obj2age]];

    return[obj1Agecompare:obj2Age];

    }];

    for(TRStudent*sinsorted)

    {

    NSLog(@"%@", s);

    }

    NSDictionary*dict =@{@"1":stu1,@"2":stu2,@"3":stu3,@"4":stu4,@"5":stu5};

    sorted = [dictkeysSortedByValueUsingComparator:^NSComparisonResult(idobj1,idobj2) {

    NSNumber*obj1Age = [NSNumbernumberWithInt:[obj1age]];

    NSNumber*obj2Age = [NSNumbernumberWithInt:[obj2age]];

    return[obj1Agecompare:obj2Age];

    }];

    for(NSString*keyinsorted)

    {

    NSLog(@"%@=%@", key, dict[key]);

    }

    相关文章

      网友评论

        本文标题:IOS 字典与数组的排序

        本文链接:https://www.haomeiwen.com/subject/ygfcmttx.html