以下为自定义的排序方式的实现


主函数测试:
#import <Foundation/Foundation.h>
#import "Person+Compare.h"
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSMutableArray *personArray = [[NSMutableArray alloc]initWithCapacity:20];
[personArray addObject:[Person personWithName:@"Tom" andAge:20]];
[personArray addObject:[Person personWithName:@"Smith" andAge:21]];
[personArray addObject:[Person personWithName:@"Jhon" andAge:19]];
[personArray addObject:[Person personWithName:@"Bruth" andAge:24]];
//按姓名排序
NSMutableArray *sortedByName = [NSMutableArray arrayWithArray:[personArray sortedArrayUsingSelector:@selector(CompareByName:)]];
NSLog(@"%@",sortedByName);
//按年龄排序
NSMutableArray *sortedByAge = [NSMutableArray arrayWithArray:[personArray sortedArrayUsingSelector:@selector(CompareByAge:)]];
NSLog(@"%@",sortedByAge);
}
return 0;
}
测试结果为:


程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4740195.html,如需转载请自行联系原作者