1.派生

 voidarrayNew()
{
NSArray*array = [NSArrayarrayWithObjects:@"",@"",nil];
NSArray*array2 = [arrayarrayByAddingObject:@""];
NSLog(@"%@", array2); NSArray*array4 = [NSArrayarrayWithObjects:@"",@"",@"",nil];
NSRangerange =NSMakeRange(,);
NSArray*array5 = [array4subarrayWithRange:range];
NSLog(@"%@", array5);
}
 
2.IO文件读写
需要符合XML格式
(1)写入文件
 voidarrayOther()
{
NSArray *array = [NSArray arrayWithObjects:@"",@"",@"",@"",nil];
NSString *str = [array componentsJoinedByString:@","];
NSLog(@"%@", str); [array writeToFile:@"/Users/hellovoidworld/Study/Foundation/NSArray2/array.txt"atomically:YES];
}
 
(2)读取文件
 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);
}
 
3.数组排序
(1)指定元素的排序方法进行排序
         // 默认的排序方法
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);
 
(2)对自定义类型进行排序
Student.m
 @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);
}
 
(3)使用block进行排序
 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);
}
 
(4)使用描述器进行排序
 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排序的更多相关文章

  1. [OC Foundation框架 - 7] NSArray的创建与遍历

    NSArray是不可变的,不能先创建再添加元素 NSArray可以放入任何OC对象,但不能放入基本数据类型.结构体.枚举等非OC对象 不能存储nil   A.常用方法1 创建 返回用量 是否含有某元素 ...

  2. Foundation框架之NSArray、NSDictionary、NSSet及其Mutable类型

    Foundation框架之NSArray.NSDictionary.NSSet及其Mutable类型 目录 概述——对Foundation框架集合类的理解 NSArray NSDictionary N ...

  3. OC Foundation框架—集合

    Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...

  4. OC Foundation框架—字符串

    一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:OC不可变数组 NSMutableA ...

  5. iOS - OC Foundation 框架

    前言 框架是由许多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使研发程序更容易. Foundation 框架:为所有程序开发奠定基础的框架称为 Foundation 框架. Cocoa :是指 ...

  6. OC Foundation框架—结构体

    一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...

  7. 黑马程序员——OC语言Foundation框架 (2) NSArray NSSet NSDictionary\NSMutableDictionary

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)NSArray 1>NSArray :不可变数组 ①创建方法 ...

  8. OC中Foundation框架之NSArray、NSMutableArray

    NSArray概述 NSArray是OC中的数组类 NSArray特点 )只能存放任意OC对象,并且是有顺序的 )不能存放非OC对象,比如int/float/double/char/enum/stru ...

  9. [OC Foundation框架 - 20] 统计代码行数

    注意: 1.变量名和函数名不要混淆调用 2.不要对文件夹进行文件的操作,没有权限 3.递归调用注意初始化变量   // // main.m // CodeLineCount // // Created ...

随机推荐

  1. Qt之界面数据存储与获取(使用setUserData()和userData())

    在GUI开发中,往往需要在界面中存储一些有用的数据,这些数据可以来配置文件.注册表.数据库.或者是server. 无论来自哪里,这些数据对于用户来说都是至关重要的,它们在交互过程中大部分都会被用到,例 ...

  2. POJ2531——Network Saboteur(随机化算法水一发)

    Network Saboteur DescriptionA university network is composed of N computers. System administrators g ...

  3. Java基于Servlet过虑器

  4. 8款替代Dreamweaver的开源网页开发工具

    Adobe Dreamweaver虽然非常好用,但它并不是唯一一个能够设计.开发.发布精彩网站的Web开发集成环境.我们的开源世界里有很多非常棒的可以完全替代Dreamweaver的各种功能的优秀We ...

  5. 中断服务程序不用interrupt关键字也可实现中断,该关键字是否必须?

    2013-06-20 11:13:35 中断服务程序不用interrupt关键字也可实现中断,该关键字是否必须? 使用tools->pin connect,将INT5与pin.txt关联,模拟外 ...

  6. 【HDOJ】4305 Lightning

    1. 题目描述当一个结点lightning后,可以向其周围距离小于等于R的结点传播lightning.然后以该结点为中心继续传播.以此类推,问最终形成的树形结构有多少个. 2. 基本思路生成树级数模板 ...

  7. hibernate实体的几种状态:

    hibernate实体的几种状态: 实体的生命周期中,实体主要经过瞬时(Transient),托管(Attatched或Managed),游离(Detached)和销毁(Removed)四个状态. 瞬 ...

  8. acdream 小晴天老师系列——苹果大丰收(DP)

    小晴天老师系列——苹果大丰收 Problem Description 小晴天的后花园有好多好多的苹果树,某天,苹果大丰收~小晴天总共摘了M个苹果,我们假设苹果之间是不可分辨的. 为了保存苹果,小晴天买 ...

  9. (转载)C++lambda表达式

    (转载)http://www.cnblogs.com/L-hq815/archive/2012/08/23/2653002.html lambda表达式 C++ 语言中的lambda表达式在很多情况下 ...

  10. Swift不可变数组

    Objective-C编写了2个不同的类来区分不可变数组(NSArray)和可变数组(NSMutableArray): Swift通过使用常量和变量来区分不可变数组和可变数组. 只要将数组定义为常量, ...