NSArray的排序

时间:2023-03-10 06:21:17
NSArray的排序

1、自定义方法排序:

NSArray *array = [NSArray arrayWithObjects:@"", @"", @"", @"", nil];
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
//sortedArrayUsingSelector:@selector() 方法会把数组array中的元素按指定元素的比较方法compare: 返回一个排好序的数组(默认从小到大),原数组的元素顺序不会改变;
//注:sortedArrayUsingSelector:@selector()的参数是传一个方法参数。 //先在其他类中实现如下方法:Start
+ (id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname { //此方法创建对象时调用
Student *stu = [[[Student alloc] init] autorelease];
stu.lastname = lastname;
stu.firstname = firstname;
return stu;
}
-(NSComparisonResult)compareStudent:(Student *)stu
{ // 先按照姓排序
NSComparisonResult result = [self.lastname compare:stu.lastname];
if (result == NSOrderedSame) { //默认按照ASCII码表中的顺序排序,如果有相同的姓,就比较名字。
result = [self.firstname compare:stu.firstname];
}
return result;
}
+ (id)studentWithFirstname:(NSString *)firstname lastname:(NSString *)lastname bookName:(NSString *)bookName {
Student *stu = [Student studentWithFirstname:firstname lastname:lastname];
stu.book = [Book bookWithName:bookName];
return stu;
}
+ (id)bookWithName:(NSString *)name {
Book *book = [[[Book alloc] init] autorelease];
book.name = name;
return book;
}
-(NSString *)description
{
//重写description方法是输出结果从内存地址变为可读的对象名如:Li MingeJie
return [NSString stringWithFormat:@"[%@ %@-%@]", self.lastname, self.firstname, self.book.name];
}
//先在其他类中实现如上方法:End Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];
Student *stu2 = [Student studentWithFirstname:@"XiaoXu" lastname:@"Zhang"];
Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];
Student *stu4 = [Student studentWithFirstname:@"LiDa" lastname:@"Zhang"];
NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
//创建的四个对象存进数组array中。
NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)]; //指定排序的比较方法compareStudent: ,这个方法为在类中实现的方法。

2、数组集合利用block进行排序:

Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];
Student *stu2 = [Student studentWithFirstname:@"XiaoXu" lastname:@"Zhang"];
Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];
Student *stu4 = [Student studentWithFirstname:@"LiDa" lastname:@"Zhang"]; NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
//创建的四个对象存进数组array中。 NSArray *array2 = [array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2)
{//sortedArrayUsingComparator: 方法 是利用block进行排序,每当进行两个元素的比较时都会调用这个Block进行比较;Block为sortedArrayUsingComparator: 方法的参数。
NSComparisonResult result = [obj1.lastname compare:obj2.lastname];  // 先按照姓排序 if (result == NSOrderedSame) {//默认按照ASCII码表中的顺序排序,如果有相同的姓,就比较名字
result = [obj1.firstname compare:obj2.firstname];
}
return result;
}];

3、数组排序-高级排序:

Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"];
Student *stu2 = [Student studentWithFirstname:@"XiaoXu" lastname:@"Zhang" bookName:@"book2"];
Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"];
Student *stu4 = [Student studentWithFirstname:@"LiDa" lastname:@"Zhang" bookName:@"book1"]; NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil]; NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
//NSSortDescriptor 为排序描述类型,bookNameDesc 为排序描述类型对象;sortDescriptorWithKey: 方法先按照指定的书名@"book.name"进行排序, 这里的key(book.name中的book)写的是@property的名称;ascending:代表升序。 //再按照姓进行排序:
NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES]; //再按照名进行排序:
NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES]; //按顺序添加排序描述器descs:bookNameDesc、lastnameDesc、firstnameDesc可根据实际需求按顺序写在代码中,决定着排序的优先级,先写的先按此标准排序:
NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil];//此处的descs是一个设置好顺序的顺序描述器,而非寻常数组。 NSArray *array2 = [array sortedArrayUsingDescriptors:descs];
//sortedArrayUsingDescriptors: 方法把排序描述器descs按其内部已经指定好的顺序排好顺序并返回一个排好顺序的数组。 NSLog(@"array2:%@", array2);//此处的数组array2是一个按需求排好顺序的新数组。