系统类型排序;

NSArray *goodsNames =@[@"computer",@"iphone",@"ipad"];

NSArray *sortedArray=[goodsNames sortedArrayUsingSelector:@selector(compare:)];

自定义排序

Person *p1=[[Person alloc]initWithName:@"tom" andAge:12 andCard:nil];

Person *p2=[[Person alloc]initWithName:@"jack" andAge:23 andCard:nil];

Person *p1=[[Person alloc]initWithName:@"maggie" andAge:11 andCard:nil];

NSArry *personsArray=@[p1,p2,p3];

在person类中定义一个comparePerson方法

/***************************************************************

-(NSComparisonResult)comparePerson:(Person *)person

{

    NSNumber *age1=[[NSNumber alloc]initWithInt:self.age];

    NSnumber *age2=[[NSNumber alloc]initWithInt:person.age];

  NSComparisonResult result=[age1 compare:age2];

  if(result == NSOrderedSame)

    {

    result=[self.name compare:person.name];

    }

  return result;

}

/******************************************************************

NSArray *personsArray=@[p1,p2,p3];

NSArray *sortedArray=[personsArray sortedArrayUsingSelector:@selector(comparePerson:)];

完成对personsArray进行排序

使用block进行排序

NSArray *sortedArray=[personsArray sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2){

Person *p1=(Person *)obj1;

Person *p2=(Person *)obj2;

NSNumber *age1=[[NSNumber alloc]initWithInt:self.age];

    NSnumber *age2=[[NSNumber alloc]initWithInt:person.age];

  NSComparisonResult result=[age1 compare:age2];

  if(result == NSOrderedSame)

    {

    result=[self.name compare:person.name];

    }

  return result;

}

复杂排序

NSSortDescriptor

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

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

NSArray *conditionsArray=@[sort1,sort2];

NSArray *sortedArray =[personsArray sortedArrayUsingDescriptors:conditionsArray];

/*******************

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

NSSortDescriptor *sort2=[[NSSortDescriptor]initWithKey:@"self.card.balance" ascending:YES];

Object-C-Foundation-数组排序的更多相关文章

  1. (译)Getting Started——1.3.3 Working with Foundation(使用Foundation框架)

    在你使用Objective-C语言开发应用时,你会发现在开发中,你会用到很多框架.尤其是Foundation框架,该框架为应用提供了最基础的服务.Foundation框架包括了代表着基本数据类型的va ...

  2. 从今天開始学习iOS开发(iOS 7版)--实现一款App之Foundation框架的使用

    iOSFoundation框架 当你着手为你的应用编写代码的时候,你会发现有很多可供使用的Objective-C的框架类,当中尤其重要的就是基础框架类.它为平台全部的应用提供基础服务.基础框架类中包括 ...

  3. ios 常用字符串的操作

    //将NSData转化为NSString        NSString* str = [[NSString alloc] initWithData:response encoding:NSUTF8S ...

  4. WP8:在Cocos2d-x中使用OpenXLive

    一.    Cocos2d-x for Windows Phone 到2013年底,几大手游引擎都陆续支持WP8了,特别是Unity3D和Cocos2d-x.有过游戏开发经验的朋友们应该对这两个引擎不 ...

  5. IOS NS 字符串 数组 字典 文件 动态 静态 操作

    ios 常用字符串的操作   //将NSData转化为NSString        NSString* str = [[NSString alloc] initWithData:response e ...

  6. 【jquery】 API讲解 内部培训资料

    资料在百度云盘 一.jquery  API讲解 1.jquery  api如何使用 jquery  api http://www.hemin.cn/jq/ 2.常用api讲解 选择器: 通过$()获取 ...

  7. ios 对象的集合类(collection classes)

    当你着手为你的应用编写代码的时候,你会发现有许多可供使用的Objective-C的框架类,其中尤其重要的就是基础框架类,它为平台所有的应用提供基础服务.基础框架类中包括了表示字符串和数字等基本数据类型 ...

  8. ios 字符串的操作汇总

    //将NSData转化为NSString        NSString* str = [[NSString alloc] initWithData:response encoding:NSUTF8S ...

  9. Laravel开发采坑系列问题

    2017年12月22日17:40:03 不定时更新 版本5.4.X 一下是可能会遇到的坑 1,必须的写路由转发才能访问控制器,当然你可以自动路由访问,但是需要些匹配规则,其实还是转发了 好多人讨论过自 ...

  10. iOS kvo 结合 FBKVOController 的使用

    iOS kvo 结合 FBKVOController 的使用 一:FBKVOControlloer是FaceBook开源的一个 在 iOS,maxOS上使用 kvo的 开源库: 提供了block和@s ...

随机推荐

  1. [SQL] 命令远程恢复数据库

    sp_configure ; RECONFIGURE; GO sp_configure ; RECONFIGURE; GO EXEC master..xp_cmdshell 'net use \\70 ...

  2. html如何引用另一个html的内容

    第一种:使用jquery: https://api.jquery.com/load/ 这可以加载html中某个id的元素 $( "#result" ).load( "aj ...

  3. Windows Server 2008 R2之一活动目录服务部署

    测试环境: 服务器:计算机名Win2008R2CNDC,已安装Windows Server 2008 R2.IPV4:192.168.1.13,255.255.255.0,网关地址192.168.1. ...

  4. wpgcms---首页数据怎么掉

    在首页调用单页模型下的,单页列表使用的方法: {% for page in wpg.page.getList('business') %} <li> <div class=" ...

  5. 国外很有多优秀的HTML5前端开发框架

    国外很有多优秀的HTML5前端开发框架 相信大家都耳熟能详:JQuery Mobile,Twitter Bootstrap, Schena Touch,  BackBone等等. 同样,也存在很多国内 ...

  6. SQL语句的执行过程

    1.语法校验 如果在SQL计划缓存中没有对应的执行计划,服务器首先会对用户请求的SQL语句进行语法效验,如果有语法错误,服务器会结束查询操作,并用返回相应的错误信息给调用它的应用程序. 注意:此时返回 ...

  7. 《机器学习实战》2.2.2分析数据:使用matplotlib创建散点图

    #输出散点图 def f(): datingDataMat,datingLabels = file2matrix("datingTestSet3.txt") fig = plt.f ...

  8. URAL 1277 - Cops and Thieves - [无向图点带权的最小点割]

    题目链接:https://cn.vjudge.net/problem/URAL-1277 The Galaxy Police (Galaxpol) found out that a notorious ...

  9. hadoop(角色)各个组件配置信息

    1)namenode:        core-site.xml文件中决定:            <property>                <name>fs.def ...

  10. 学习计划 mysql desc表结构分析

    在完成数据表建表后,我们需要知道我们的表结构是什么,有没有和构造表时差异的地方. -- 简单查看表结构 desc 表名 这里拿数据库的一张表中做示例 mysql> desc rental; +- ...