[OC Foundation框架 - 8] NSArray排序
1.派生
voidarrayNew()
{
NSArray*array = [NSArrayarrayWithObjects:@"",@"",nil];
NSArray*array2 = [arrayarrayByAddingObject:@""];
NSLog(@"%@", array2); NSArray*array4 = [NSArrayarrayWithObjects:@"",@"",@"",nil];
NSRangerange =NSMakeRange(,);
NSArray*array5 = [array4subarrayWithRange:range];
NSLog(@"%@", array5);
}
voidarrayOther()
{
NSArray *array = [NSArray arrayWithObjects:@"",@"",@"",@"",nil];
NSString *str = [array componentsJoinedByString:@","];
NSLog(@"%@", str); [array writeToFile:@"/Users/hellovoidworld/Study/Foundation/NSArray2/array.txt"atomically:YES];
}
voidarrayOther()
{
NSArray*array = [NSArrayarrayWithObjects:@"",@"",@"",@"",nil];
NSString*str = [arraycomponentsJoinedByString:@","];
NSLog(@"%@", str); // [array writeToFile:@"/Users/hellovoidworld/Study/Foundation/NSArray2/array.txt" atomically:YES]; NSString*path =@"/Users/hellovoidworld/Study/Foundation/NSArray2/array.txt";
NSArray*array2 = [NSArrayarrayWithContentsOfFile:path];
NSLog(@"%@", array2);
}
// 默认的排序方法
NSArray *array = @[@"b", @"d", @"a", @"z"];
NSLog(@"排序前 %@", array); NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后 %@", array2); // 使用block排序
NSArray *array3 = @[@"z", @"", @"b", @"", @"x"];
NSArray *array4 = [array3 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
return [obj1 compare:obj2];
}];
NSLog(@"使用block排序后 %@", array4);
@implementationStudent + (Student*) initWithFirstName:(NSString*) firstName withLastName:(NSString*) lastName
{
Student*stu = [[[Studentalloc]init]autorelease];
stu.firstName = firstName;
stu.lastName= lastName;
returnstu;
} - (void)dealloc
{
[_firstNamerelease];
[_lastNamerelease];
[superdealloc];
} - (NSComparisonResult)compareStudent:(Student*) stu
{
NSComparisonResultresult = [self.lastNamecompare: stu.lastName];
if(result ==NSOrderedSame)
{
result = [self.firstNamecompare:stu.firstName];
} returnresult;
}
main.m
- (NSString*) description
{
return[NSStringstringWithFormat:@"[%@-%@]",self.firstName,self.lastName];
} @end voidarraySort2()
{
Student *stu1 = [Student initWithFirstName:@"Sinon"withLastName:@"Huang"];
Student *stu2 = [Student initWithFirstName:@"Franky"withLastName:@"Xie"];
Student *stu3 = [Student initWithFirstName:@"Mon"withLastName:@"Yao"];
Student *stu4 = [Student initWithFirstName:@"JJ"withLastName:@"Deng"]; NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4,nil];
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];
NSLog(@"%@", array2);
}
voidarraySort3()
{
Student*stu1 = [StudentinitWithFirstName:@"Sinon"withLastName:@"Huang"];
Student*stu2 = [StudentinitWithFirstName:@"Franky"withLastName:@"Xie"];
Student*stu3 = [StudentinitWithFirstName:@"Mon"withLastName:@"Yao"];
Student*stu4 = [StudentinitWithFirstName:@"JJ"withLastName:@"Deng"]; NSArray*array = [NSArrayarrayWithObjects:stu1, stu2, stu3, stu4,nil];
NSArray*array2 = [arraysortedArrayUsingComparator:^NSComparisonResult(Student*obj1,Student*obj2) {
NSComparisonResultresult = [obj1.lastNamecompare: obj2.lastName];
if(result ==NSOrderedSame)
{
result = [obj1.firstNamecompare:obj2.firstName];
} returnresult;
}]; NSLog(@"%@", array2);
}
voidarraySort4()
{
Student*stu1 = [StudentinitWithFirstName:@"Sinon"withLastName:@"Huang"withBook:[BookbookWithName:@"Jave Programming"]];
Student*stu2 = [StudentinitWithFirstName:@"Franky"withLastName:@"Xie"withBook:[BookbookWithName:@"Cook"]];
Student*stu3 = [StudentinitWithFirstName:@"Mon"withLastName:@"Yao"withBook:[BookbookWithName:@"History"]];
Student*stu4 = [StudentinitWithFirstName:@"JJ"withLastName:@"Deng"withBook:[BookbookWithName:@"Biographic"]]; NSArray*array = [NSArrayarrayWithObjects:stu1, stu2, stu3, stu4,nil]; NSSortDescriptor*desc1 = [NSSortDescriptorsortDescriptorWithKey:@"book.name"ascending:YES];
NSSortDescriptor*desc2 = [NSSortDescriptorsortDescriptorWithKey:@"lastName"ascending:YES];
NSSortDescriptor*desc3 = [NSSortDescriptorsortDescriptorWithKey:@"firstName"ascending:YES]; NSArray*array2 = [arraysortedArrayUsingDescriptors:[NSArrayarrayWithObjects:desc1, desc2, desc3,nil]]; NSLog(@"%@", array2); }
[OC Foundation框架 - 8] NSArray排序的更多相关文章
- [OC Foundation框架 - 7] NSArray的创建与遍历
NSArray是不可变的,不能先创建再添加元素 NSArray可以放入任何OC对象,但不能放入基本数据类型.结构体.枚举等非OC对象 不能存储nil A.常用方法1 创建 返回用量 是否含有某元素 ...
- Foundation框架之NSArray、NSDictionary、NSSet及其Mutable类型
Foundation框架之NSArray.NSDictionary.NSSet及其Mutable类型 目录 概述——对Foundation框架集合类的理解 NSArray NSDictionary N ...
- OC Foundation框架—集合
Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...
- OC Foundation框架—字符串
一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:OC不可变数组 NSMutableA ...
- iOS - OC Foundation 框架
前言 框架是由许多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使研发程序更容易. Foundation 框架:为所有程序开发奠定基础的框架称为 Foundation 框架. Cocoa :是指 ...
- OC Foundation框架—结构体
一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...
- 黑马程序员——OC语言Foundation框架 (2) NSArray NSSet NSDictionary\NSMutableDictionary
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)NSArray 1>NSArray :不可变数组 ①创建方法 ...
- OC中Foundation框架之NSArray、NSMutableArray
NSArray概述 NSArray是OC中的数组类 NSArray特点 )只能存放任意OC对象,并且是有顺序的 )不能存放非OC对象,比如int/float/double/char/enum/stru ...
- [OC Foundation框架 - 20] 统计代码行数
注意: 1.变量名和函数名不要混淆调用 2.不要对文件夹进行文件的操作,没有权限 3.递归调用注意初始化变量 // // main.m // CodeLineCount // // Created ...
随机推荐
- PHP基础语法2
数组 PHP有两种数组:索引数组.关联数组. 函数 自定义函数 自定义函数通过将一组代码封装起来,使代码进行复用,程序结构与逻辑更加清晰 返回值 使用return关键字可以使函数返回值,可以返回包括数 ...
- 命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Ajax”(是否缺少程序集引用?)
解放方法 右键打开这个项目引用System.Web.Mvc,如图: 将复制本地的值改为True,英文的话应该是Copy Local,这样就解决了上面的报错问题.
- POJ2109——Power of Cryptography
Power of Cryptography DescriptionCurrent work in cryptography involves (among other things) large pr ...
- 验证Android用户输入日期
如何验证用户输入的日期是有效还是无效? private Pattern pattern; private Matcher matcher; private static final String DA ...
- C# 调用WebService的方法
很少用C#动态的去调用Web Service,一般都是通过添加引用的方式,这样的话是自动成了代理,那么动态代理调用就是我们通过代码去调用这个WSDL,然后自己去生成客户端代理.更多的内容可以看下面的两 ...
- JBoss7 局域网无法访问 解决方法
JBoss7 局域网无法访问 解决方法 在standalone模式,修改/standalone/configuration/standalone.xml.如下 修改或新增一个interface. &l ...
- Oracle命令(一):Oracle登录命令
1.运行SQLPLUS工具 C:\Users\wd-pc>sqlplus 2.直接进入SQLPLUS命令提示符 C:\Users\wd-pc>sqlplus /nolog 3.以OS身份连 ...
- Task-based Asynchronous Pattern (TAP)
The Task-based Asynchronous Pattern (TAP) is based on the System.Threading.Tasks.Task and System.Thr ...
- Glimpse
给自己程序配好Glimpse. Glimpse.Mvc 有问题 遇到稍微复杂点的内套多个PartialView,内存就爆了彪1.7g,不开Glimpse一点问题都没.另外Glimpse.Nlog也有问 ...
- 数论/the first wave
线性筛素数(原来我之前学的不是线性的啊... void getprime(){ rep(i,2,nmax){ if(!vis[i]) prime[++prime[0]]=i; for(int j=1; ...