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. 吐槽C++

    个人感觉,在c++ 道路的学习路上,遇到很多的坎坷,现在回想起来,最关键一点就是 c++知识点繁杂很多,教科书很多知识点都没有提到. 但是在实际工作中,这些没有提到的知识点,却又经常会用到(或者看开源 ...

  2. Android view的requestLayout()

    public void requestLayout () Since: API Level 1 Call this when something has changed which has inval ...

  3. 【HDOJ】4579 Random Walk

    1. 题目描述一个人沿着一条长度为n个链行走,给出了每秒钟由i到j的概率($i,j \in [1,n]$).求从1开始走到n个时间的期望. 2. 基本思路显然是个DP.公式推导也相当容易.不妨设$dp ...

  4. 【HDOJ】4251 The Famous ICPC Team Again

    划分树模板题目,主席树也可解.划分树. /* 4251 */ #include <iostream> #include <sstream> #include <strin ...

  5. UVa 101 (模拟) The Blocks Problem

    题意: 有n个木块及n个木块堆,初始状态是第i个木块在第i个木块堆上.对应有四种操作,然后输出最终状态. 分析: 用一个vector<int>模拟一个木块堆,进行相应操作即可. #incl ...

  6. BZOJ_1031_[JSOI2007]_字符串加密_(后缀数组)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1031 长度为n的字符串形成环,枚举开头位置,得到n个长度为n的串,将这些串排序,然后按照顺序输 ...

  7. [NOI2005] 维修数列

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 8397  Solved: 2530 Description In ...

  8. 前端的小Demo——涉及keyCode

    以下是我的代码: <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  9. c#里BindingFlags 筛选标志

    下列 BindingFlags 筛选标志可用于定义包含在搜索中的成员: 为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static. 指定 Bin ...

  10. 通过history.pushState无刷新改变url

    通过history.pushState无刷新改变url 背景 在浏览器中改变地址栏url,将会触发页面资源的重新加载,这使得我们可以在不同的页面间进行跳转,得以浏览不同的内容.但随着单页应用的增多,越 ...