在IOS软件开发中我们需要依据NSArray中对象的成员进行排序,我们知道对象中成员比较多,因此如果我们写了多种方法根据不同的成员进行排序,代码量很大,实现起来不是很方便,因此苹果向我们提供了一种快速排序的方法,叫做
- (void)sortUsingSelector:(SEL)comparator;
本文重视讲诉原理:
我们先来看一个例子:
#import <Foundation/Foundation.h> //Dog头文件 @interface Dog : NSObject { NSString * _name; NSUInteger _age; } -(NSComparisonResult)nameCompae:(Dog *)dog; -(NSComparisonResult)ageCompae:(Dog *)dog; -(id)initWithName:(NSString *)name andWithAge:(NSUInteger)age; @property (copy,readwrite,nonatomic) NSString * name; @property (assign,readwrite,nonatomic)NSUInteger age; @end //Dog函数实现体 @implementation Dog -(NSComparisonResult)nameCompae:(Dog *)dog { if ([_name compare:[dog name]]>0) { return NSOrderedDescending; } return NSOrderedAscending; } -(NSComparisonResult)ageCompae:(Dog *)dog { NSLog(@"%ld %ld",_age,[dog age]); if (_age > [dog age]) { return NSOrderedAscending; } return NSOrderedDescending; } -(void)setAge:(NSUInteger)age { _age = age; NSLog(@"dasdsd"); } -(id)initWithName:(NSString *)name andWithAge:(NSUInteger)age { if (self = [super init]) { _name = name; _age = age; } return self; } @end //main函数 int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableArray * dogArray = [[NSMutableArray alloc]init]; Dog * dog1 = [[Dog alloc]initWithName:@"xiaobao" andWithAge:12]; Dog * dog2 = [[Dog alloc]initWithName:@"zhansan" andWithAge:8]; Dog * dog3 = [[Dog alloc]initWithName:@"lisi" andWithAge:16]; Dog * dog4 = [[Dog alloc]initWithName:@"wangwu" andWithAge:7]; [dogArray addObject:dog1]; [dogArray addObject:dog2]; [dogArray addObject:dog3]; [dogArray addObject:dog4]; for (Dog * temp in dogArray) { NSLog(@"%@ %ld",temp.name,temp.age); } [dogArray sortUsingSelector:@selector(nameCompae:)]; for (Dog * temp in dogArray) { NSLog(@"%@ %ld",temp.name,temp.age); } [dogArray sortUsingSelector:@selector(ageCompae:)]; for (Dog * temp in dogArray) { NSLog(@"%@ %ld",temp.name,temp.age); } } return 0; }
在上面的这个例子中,我将小狗对象存入dogArray中,而该对象中又有两个成员变量,一个是狗的名字,另一个是狗的年龄,我们通过这两个属性对这个dogArray中得对象进行排序,在测试中我发现在调用sortUsingSelector函数时,选择器(即ageCompae:和nameCompae)是被多次调用的,因此我猜测,该函数的伪代码如下:
- (void)mysortUsingSelector:(SEL)comparator:①
{
int temp = 0;
for (int i=0; i<[self count]-1; i++)②
{ temp = i;
for (int j=i+1; j<[self count]; j++)
{
if ([[self objectAtIndex:temp] comparator:[self objectAtIndex:j]]==NSOrderedDescending)③
{
temp = j;
}
}
if (i!=temp)
{
[dogArray exchangeObjectAtIndex:i withObjectAtIndex:temp];
}
}
}
上述代码中又三点需要理解,只要理解了这个伪代码,你就知道sortUsingSelector这个函数是怎么工作的了.
①:(SEL)comparator:这个东西其实就是模仿C语言中的函数指针,我们想要使用什么函数对NSArray的对象只要把函数入口地址传给它就好了,例如上述代码中想使用dog名字进行排序,只要把nameCompae:传给它就好了,而想要按照年龄排序就传ageCompare:如果你理解函数指针,这个对你就很简单了,呵呵
②:这个self其实就是数组的地址,对应与上面代码中的dogArray
③:使用传递过来的函数进行比较,也就是C语言中函数指针的回调函数,上述代码中有一点很重要,也就是
if ([[self objectAtIndex:temp] comparator:[self objectAtIndex:j]]==NSOrderedDescending)这有在这个语句为真的时候才会进行交换,我测试多次sortUsingSelector这个函数在回调(SEL)comparator:函数时,只有在(SEL)comparator:函数返回值为NSOrderedDescending:才会进行排序,对于其它值则不会进行如何处理,这也很好的解释了我上面的if语句的书写方式是正确的.
总结:相信到了这里你一定明白了sortUsingSelector的工作方式了,如果不明白建议看看关于函数指针的视频或者书籍,推荐在51CTO上面找找;