[OC Foundation框架 - 8] NSArray排序
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 }