[OC Foundation框架 - 8] NSArray排序

时间:2021-07-16 01:36:24

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); }