iOS Developer

IOS 字典与数组的排序

2016-12-03  本文已影响38人  JakieZhang

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

}

上一篇下一篇

猜你喜欢

热点阅读