下面是老师给的例子:
main.m文件
NSArray * nameArr = @[@"Jack",@"Tom",@"Jimmy",@"Lily",@"Lucy"];
NSMutableArray * dogsArray = [[NSMutableArray alloc] init];
for (NSString * str in nameArr) {
Dog * dog = [[Dog alloc] init];
dog.name = str;
dog.age = arc4random()%10+1;
[dogsArray addObject:dog];
}
[dogsArray show];
NSArray * array = [dogsArray sortedArrayBySel:@selector(isAgeGreaterThanAnotherDog:)];
[array show];
Dog.m文件里有一个方法
- (BOOL)isAgeGreaterThanAnotherDog:(Dog *)dog {
return self.age > dog.age;
}