[OC Foundation框架 - 8] NSArray排序

本文详细介绍了NSArray的基本操作,包括数组派生、IO文件读写、数组排序等。在数组排序部分,不仅介绍了如何对基本类型进行排序,还展示了如何对自定义类型进行排序,并提供了使用block和描述器进行排序的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.派生

复制代码
 1 voidarrayNew()
 2 {
 3    NSArray*array = [NSArrayarrayWithObjects:@"1",@"2",nil];
 4    NSArray*array2 = [arrayarrayByAddingObject:@"3"];
 5    NSLog(@"%@", array2);
 6    
 7    
 8    NSArray*array4 = [NSArrayarrayWithObjects:@"1",@"2",@"3",nil];
 9    NSRangerange =NSMakeRange(1,2);
10    NSArray*array5 = [array4subarrayWithRange:range];
11    NSLog(@"%@", array5);
12 }
复制代码
 
2.IO文件读写
需要符合XML格式
(1)写入文件
复制代码
1 voidarrayOther()
2 {
3     NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",nil];
4     NSString *str = [array componentsJoinedByString:@","];
5     NSLog(@"%@", str);
6    
7     [array writeToFile:@"/Users/hellovoidworld/Study/Foundation/NSArray2/array.txt"atomically:YES];
8 }
复制代码
 
(2)读取文件
复制代码
 1 voidarrayOther()
 2 {
 3    NSArray*array = [NSArrayarrayWithObjects:@"1",@"2",@"3",@"4",nil];
 4    NSString*str = [arraycomponentsJoinedByString:@","];
 5    NSLog(@"%@", str);
 6    
 7 //    [array writeToFile:@"/Users/hellovoidworld/Study/Foundation/NSArray2/array.txt" atomically:YES];
 8    
 9    NSString*path =@"/Users/hellovoidworld/Study/Foundation/NSArray2/array.txt";
10    NSArray*array2 = [NSArrayarrayWithContentsOfFile:path];
11    NSLog(@"%@", array2);
12 }
复制代码
 
3.数组排序
(1)指定元素的排序方法进行排序
复制代码
 1         // 默认的排序方法
 2         NSArray *array = @[@"b", @"d", @"a", @"z"];
 3         NSLog(@"排序前 %@", array);
 4        
 5         NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
 6         NSLog(@"排序后 %@", array2);
 7        
 8         // 使用block排序
 9         NSArray *array3 = @[@"z", @"4", @"b", @"3", @"x"];
10         NSArray *array4 = [array3 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)    
11         {
12             return [obj1 compare:obj2];
13         }];
14         NSLog(@"使用block排序后 %@", array4);
复制代码
 
(2)对自定义类型进行排序
Student.m
复制代码
 1 @implementationStudent
 2 
 3 + (Student*) initWithFirstName:(NSString*) firstName withLastName:(NSString*) lastName
 4 {
 5    Student*stu =  [[[Studentalloc]init]autorelease];
 6     stu.firstName  = firstName;
 7     stu.lastName= lastName;
 8    returnstu;
 9 }
10 
11 - (void)dealloc
12 {
13     [_firstNamerelease];
14     [_lastNamerelease];
15     [superdealloc];
16 }
17 
18 - (NSComparisonResult)compareStudent:(Student*) stu
19 {
20    NSComparisonResultresult =  [self.lastNamecompare: stu.lastName];
21    if(result ==NSOrderedSame)
22     {
23         result = [self.firstNamecompare:stu.firstName];
24     }
25    
26    returnresult;
27 }
复制代码

main.m
复制代码
 1 - (NSString*) description
 2 {
 3    return[NSStringstringWithFormat:@"[%@-%@]",self.firstName,self.lastName];
 4 }
 5 
 6 @end
 7  
 8 voidarraySort2()
 9 {
10     Student *stu1 = [Student initWithFirstName:@"Sinon"withLastName:@"Huang"];
11     Student *stu2 = [Student initWithFirstName:@"Franky"withLastName:@"Xie"];
12     Student *stu3 = [Student initWithFirstName:@"Mon"withLastName:@"Yao"];
13     Student *stu4 = [Student initWithFirstName:@"JJ"withLastName:@"Deng"];
14    
15     NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4,nil];
16     NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];
17     NSLog(@"%@", array2);
18 }
复制代码
 
(3)使用block进行排序
复制代码
 1 voidarraySort3()
 2 {
 3    Student*stu1 = [StudentinitWithFirstName:@"Sinon"withLastName:@"Huang"];
 4    Student*stu2 = [StudentinitWithFirstName:@"Franky"withLastName:@"Xie"];
 5    Student*stu3 = [StudentinitWithFirstName:@"Mon"withLastName:@"Yao"];
 6    Student*stu4 = [StudentinitWithFirstName:@"JJ"withLastName:@"Deng"];
 7    
 8    NSArray*array = [NSArrayarrayWithObjects:stu1, stu2, stu3, stu4,nil];
 9    NSArray*array2 = [arraysortedArrayUsingComparator:^NSComparisonResult(Student*obj1,Student*obj2) {
10        NSComparisonResultresult =  [obj1.lastNamecompare: obj2.lastName];
11        if(result ==NSOrderedSame)
12         {
13             result = [obj1.firstNamecompare:obj2.firstName];
14         }
15        
16        returnresult;
17     }];
18                       
19    NSLog(@"%@", array2);
20 }
复制代码
 
(4)使用描述器进行排序
复制代码
 1 voidarraySort4()
 2 {
 3    Student*stu1 = [StudentinitWithFirstName:@"Sinon"withLastName:@"Huang"withBook:[BookbookWithName:@"Jave Programming"]];
 4    Student*stu2 = [StudentinitWithFirstName:@"Franky"withLastName:@"Xie"withBook:[BookbookWithName:@"Cook"]];
 5    Student*stu3 = [StudentinitWithFirstName:@"Mon"withLastName:@"Yao"withBook:[BookbookWithName:@"History"]];
 6    Student*stu4 = [StudentinitWithFirstName:@"JJ"withLastName:@"Deng"withBook:[BookbookWithName:@"Biographic"]];
 7    
 8    NSArray*array = [NSArrayarrayWithObjects:stu1, stu2, stu3, stu4,nil];
 9    
10    NSSortDescriptor*desc1 = [NSSortDescriptorsortDescriptorWithKey:@"book.name"ascending:YES];
11    NSSortDescriptor*desc2 = [NSSortDescriptorsortDescriptorWithKey:@"lastName"ascending:YES];
12    NSSortDescriptor*desc3 = [NSSortDescriptorsortDescriptorWithKey:@"firstName"ascending:YES];
13    
14    
15    NSArray*array2 = [arraysortedArrayUsingDescriptors:[NSArrayarrayWithObjects:desc1, desc2, desc3,nil]];
16    
17    NSLog(@"%@", array2);
18    
19 }
复制代码

转载于:https://www.cnblogs.com/wvqusrtg/p/4504515.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值