[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 ...
随机推荐
- mysql-5.7.10-winx64 安装时遇到的问题
1.修改密码# /etc/init.d/mysqld stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking &am ...
- java ServerSocket服务端编程
public class Test { public static void main(String[] args) throws Exception{ //1. 构造ServerSocket实例,指 ...
- Ubuntu 12.04搭建Andorid编译环境
1.安装JDK,Android 5.0开始,开始使用OpenJDK 1.7,4.4等低版本是Oracke JDK1.6 install java environment // install open ...
- 【转】Ubuntu乱码解决方案(全)
转自:http://www.cnblogs.com/end/archive/2011/04/19/2021507.html ubuntu下中文乱码解决方案(全) 1.ibus输入法 Ubuntu 系统 ...
- Fast scroller styles
<!-- Fast scroller styles --> <!-- Drawable to use as the fast scroll thumb. --> <att ...
- Windows 各种计时函数总结
本文对Windows平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的 5种方法.分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及cloc ...
- poj 1789 Truck History(最小生成树)
模板题 题目:http://poj.org/problem?id=1789 题意:有n个型号,每个型号有7个字母代表其型号,每个型号之间的差异是他们字符串中对应字母不同的个数d[ta,tb]代表a,b ...
- It is indirectly referenced from required .class files
配置eclipse的导入包,有些包引用的位置不正确
- MySql命令的基本操作
MySQL的基本命令: 登录:mysql -h -u root -p password:*****; 例如:mysql -h127.0.0.1 -uroot -p 增加一个用户: grant all ...
- Samba 'smbcacls'命令安全绕过漏洞
漏洞版本: Samba 4.x 漏洞描述: Bugtraq ID:66232 CVE ID:CVE-2013-6442 Samba是一款实现SMB协议.跨平台进行文件共享和打印共享服务的程序. 当使用 ...