NSMutableArray中各种自定义排序方法
/*
(1)sortedArrayUsingComparator调用NScomparator方法
*/
FirstMethod.m-(void) test2{
//声明一个数组
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"123",@"23",@"45",@"67", nil];
NSMutableString *outputBefor = [[NSMutableString alloc] init];
for (NSString *str in sortArray) {
[outputBefor appendFormat:@"%@ ",str];
}
NSLog(@"排序前:%@",outputBefor);
NSArray *arr = [sortArray sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
}
/*
利用sortedArrayUsingFunction调用对应方法customSort
这个方法中的Obje1和obj2分别是指数组中的对象
*/
SecondMethod.m
NSInteger customSort(id obj1,id obj2){
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}
-(void)twotest1{
NSArray *sortArray = [[NSArray alloc] initWithObjects:@"325",@"4516",@"457",@"325", nil];
NSMutableString *outputBefor = [[NSMutableString alloc] init];
for (NSString *str in sortArray) {
[outputBefor appendFormat:@"%@ ",str];
}
NSLog(@"排序前: %@",outputBefor);
NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil];
NSMutableString *outputAfter = [[NSMutableString alloc] init];
for (NSString *str in array) {
[outputAfter appendFormat:@"%@ ",str];
}
NSLog(@"排序后%@",outputAfter);
}
/*
利用sortUsingDescriptors调用NSSortDescriptor
我们以数组的排序为例(也许NSSortDescriptor最常用的地方是NSFetchedResultsController中,但用法大致相同)。
假设要对userArray数组中的对象进行排序,而数组中含有多个User对象(User继承于NSManagedObject),User中有一个属性叫做country。
[plain]
1. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"country" ascending:YES];
2. [userArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
这样,就可以根据每一个User的country来进行升序或降序的排列,sortUsingDescriptors的参数是一个数组,也就是说可以使用多个排序参数。
再比如说,User有一个关系叫做image,image有个属性叫做timestamp,如果要根据User的image关系的timestamp排序,仅需把上面代码中的country改为,image.timestamp。
*/
Score.h
@interface Score : NSObject
@property(nonatomic,assign)NSInteger score;
-(id)initWithScore:(NSInteger)score;
@end
Score.m
@implementation Score
-(id)initWithScore:(NSInteger)score{
self = [super self];
if (self) {
_score = score;
}
return self;
}
@end
Student.h
#include "Score.h"
@interface Student : NSObject
@property (nonatomic,copy)NSString *name;
@property(nonatomic,copy)Score *stuScore;
-(id)initWithName:(NSString *)name andScore:(NSInteger)score;
@end
@implementation Student
-(id)initWithName:(NSString *)name andScore:(NSInteger)score{
self = [super self];
if (self) {
_name = name;
_stuScore = [[Score alloc] initWithScore:score];
}
return self;
}
@end
ThirdMethod.h@interface three : NSObject
-(void)sortedPrint;
@end
@implementation three
-(void)sortedPrint{
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 9; i ++) {
NSInteger random = arc4random() % 21;
Student *stu = [[Student alloc] initWithName:@"luo" andScore:random];
[array addObject:stu];
}
NSLog(@"%@",array);
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"stuScore.score" ascending:YES];
[array sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
NSLog(@"%@",array);
}
@end