您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页IOS 字典与数组的排序

IOS 字典与数组的排序

来源:伴沃教育

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]);

}

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务