Object-C-Foundation-数组排序
系统类型排序;
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-数组排序的更多相关文章
- (译)Getting Started——1.3.3 Working with Foundation(使用Foundation框架)
在你使用Objective-C语言开发应用时,你会发现在开发中,你会用到很多框架.尤其是Foundation框架,该框架为应用提供了最基础的服务.Foundation框架包括了代表着基本数据类型的va ...
- 从今天開始学习iOS开发(iOS 7版)--实现一款App之Foundation框架的使用
iOSFoundation框架 当你着手为你的应用编写代码的时候,你会发现有很多可供使用的Objective-C的框架类,当中尤其重要的就是基础框架类.它为平台全部的应用提供基础服务.基础框架类中包括 ...
- ios 常用字符串的操作
//将NSData转化为NSString NSString* str = [[NSString alloc] initWithData:response encoding:NSUTF8S ...
- WP8:在Cocos2d-x中使用OpenXLive
一. Cocos2d-x for Windows Phone 到2013年底,几大手游引擎都陆续支持WP8了,特别是Unity3D和Cocos2d-x.有过游戏开发经验的朋友们应该对这两个引擎不 ...
- IOS NS 字符串 数组 字典 文件 动态 静态 操作
ios 常用字符串的操作 //将NSData转化为NSString NSString* str = [[NSString alloc] initWithData:response e ...
- 【jquery】 API讲解 内部培训资料
资料在百度云盘 一.jquery API讲解 1.jquery api如何使用 jquery api http://www.hemin.cn/jq/ 2.常用api讲解 选择器: 通过$()获取 ...
- ios 对象的集合类(collection classes)
当你着手为你的应用编写代码的时候,你会发现有许多可供使用的Objective-C的框架类,其中尤其重要的就是基础框架类,它为平台所有的应用提供基础服务.基础框架类中包括了表示字符串和数字等基本数据类型 ...
- ios 字符串的操作汇总
//将NSData转化为NSString NSString* str = [[NSString alloc] initWithData:response encoding:NSUTF8S ...
- Laravel开发采坑系列问题
2017年12月22日17:40:03 不定时更新 版本5.4.X 一下是可能会遇到的坑 1,必须的写路由转发才能访问控制器,当然你可以自动路由访问,但是需要些匹配规则,其实还是转发了 好多人讨论过自 ...
- iOS kvo 结合 FBKVOController 的使用
iOS kvo 结合 FBKVOController 的使用 一:FBKVOControlloer是FaceBook开源的一个 在 iOS,maxOS上使用 kvo的 开源库: 提供了block和@s ...
随机推荐
- Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGBitmapinfo' (aka) 'enum CGBitmapInfo')
The constants for specifying the alpha channel information are declared with the CGImageAlphaInfo ty ...
- SSH 服务器不用密码
1 A: user assh-keygen -t rsa 2 Created a directory on B, if ~/.ssh exists, which is fine.a@A:~> s ...
- [Key] RegCure Pro
Serial RegCure Pro : 4A803-C4F23-422B6-1F3D6 http://www.paretologic.com/product/regcure-pro/
- jQuery里面ajax请求的封装
为了避免ajax漫天飞,我们需要对jQuery的代码进行封装,封装代码: function api_request(name, params, cb, scope, async, el) { if ( ...
- 9.11 Django视图 view和路由
2018-9-11 16:34:16 2018-9-11 19:00:24 越努力,.越幸运! Django框架参考: https://www.cnblogs.com/liwenzhou/p/8296 ...
- Spring接受前台的数据超过256出现如下异常:
转载自:http://blog.csdn.net/dracotianlong/article/details/47604723 Spring接受前台的数据超过256出现如下异常: org.spring ...
- R数据可视化手册学习——条形图
1. 绘制简单条形图 # 使用ggplot2和gcookbook library(ggplot2); library(gcookbook) g <- ggplot(data = pg_mean, ...
- three levels of abstraction
DATABASESYSTEM CONCEPTS SIXTH EDITION Abraham Silberschatz Yale University Henry F. KorthLehigh Univ ...
- supervisor control in centos 6/7 python2.6.2.7 3.4
sudo yum install epel-releasesudo yum install python34 sudo pip install virtualenv yum -y install ep ...
- 网站优化不等于搜索引擎优化SEO
对于SEO相信搞网络营销的人基本上都知道这个名词,英文全称为search engine optimization,中文一般叫搜索引擎优化,也有的叫搜索引擎定位(Search Engine Positi ...