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

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 数组可对其中包含的元素进行排序。 在排序前,我们需要定义一个Model类,将Model类对象添加至数组中。 定义一...
    SkyMing一C阅读 13,730评论 0 15
  • 排序规则描述类: NSSortDescriptor NSSortDescriptor来指定排序规则, 对集合等进行...
    SkyMing一C阅读 11,629评论 3 12
  • 01-常量与变量 学习swift第一步打印Hello World print("Hello World") swi...
    iOS_恒仔阅读 10,637评论 2 19
  • 整理出的一些简单实用的OC笔试题,如有错误之处希望大家及时提出,以便修改,不误人子弟.1、不会立刻使引用计数器改变...
    李xiao屁的忧伤阅读 8,116评论 1 20
  • 面向对象三大支柱封装:就是把相关的数据和代码结合成一个有机的整体,形成数据和操作代码的封装体,对外只提供一个可以控...
    WangDavid阅读 5,679评论 0 2