测试文件:main.m
NSInteger compare(Animal *animal1,Animal *animal2,void *p)
{
if (animal1.weight<animal2.weight) {
return NSOrderedDescending;
}
if (animal1.weight==animal2.weight) {
return NSOrderedSame;
}
else
return NSOrderedAscending;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *arr = [[NSArray alloc] initWithObjects:@1, @3, @2, @4, nil];//创建并初始化数组
NSArray *sortArr = [arr sortedArrayUsingSelector:@selector(compare:)];//基本数据类型数组排序
NSLog(@"%@",sortArr);
Animal *animal = [[Animal alloc] initWithWeight:3];
Animal *animal1 = [[Animal alloc] initWithWeight:2];
Animal *animal2 = [[Animal alloc] initWithWeight:1];
Animal *animal3 = [[Animal alloc] initWithWeight:4];
NSArray *arr1 = @[animal,animal1,animal2,animal3];//创建并初始化(数组成员为对象)的数组
NSArray *sortArr1 = [arr1 sortedArrayUsingSelector:@selector(compareWithWeight:)];//用实例方法排序
NSLog(@"%@",sortArr1);
NSArray *sortArr2 = [arr1 sortedArrayUsingFunction:compare context:NULL];//用函数排序
NSLog(@"%@",sortArr2);
//用block排序
NSArray *sortArr3 = [arr1 sortedArrayUsingComparator:^NSComparisonResult(Animal *animal1,Animal *animal2){
if (animal1.weight>animal2.weight) {
return NSOrderedAscending;
}
if (animal1.weight<animal2.weight) {
return NSOrderedDescending;
}
else
return NSOrderedSame;
}];
NSLog(@"%@",sortArr3);
}
return 0;
}
@interface Animal : NSObject
@property (nonatomic,assign) int weight;
- (instancetype)initWithWeight:(int)weight;
- (NSComparisonResult) compareWithWeight:(Animal *)another;
- (NSString *) description;
@end
#import <Foundation/Foundation.h>
#import "Animal.h"NSInteger compare(Animal *animal1,Animal *animal2,void *p)
{
if (animal1.weight<animal2.weight) {
return NSOrderedDescending;
}
if (animal1.weight==animal2.weight) {
return NSOrderedSame;
}
else
return NSOrderedAscending;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *arr = [[NSArray alloc] initWithObjects:@1, @3, @2, @4, nil];//创建并初始化数组
NSArray *sortArr = [arr sortedArrayUsingSelector:@selector(compare:)];//基本数据类型数组排序
NSLog(@"%@",sortArr);
Animal *animal = [[Animal alloc] initWithWeight:3];
Animal *animal1 = [[Animal alloc] initWithWeight:2];
Animal *animal2 = [[Animal alloc] initWithWeight:1];
Animal *animal3 = [[Animal alloc] initWithWeight:4];
NSArray *arr1 = @[animal,animal1,animal2,animal3];//创建并初始化(数组成员为对象)的数组
NSArray *sortArr1 = [arr1 sortedArrayUsingSelector:@selector(compareWithWeight:)];//用实例方法排序
NSLog(@"%@",sortArr1);
NSArray *sortArr2 = [arr1 sortedArrayUsingFunction:compare context:NULL];//用函数排序
NSLog(@"%@",sortArr2);
//用block排序
NSArray *sortArr3 = [arr1 sortedArrayUsingComparator:^NSComparisonResult(Animal *animal1,Animal *animal2){
if (animal1.weight>animal2.weight) {
return NSOrderedAscending;
}
if (animal1.weight<animal2.weight) {
return NSOrderedDescending;
}
else
return NSOrderedSame;
}];
NSLog(@"%@",sortArr3);
}
return 0;
}
接口文件:Animal.h
#import <Foundation/Foundation.h>@interface Animal : NSObject
@property (nonatomic,assign) int weight;
- (instancetype)initWithWeight:(int)weight;
- (NSComparisonResult) compareWithWeight:(Animal *)another;
- (NSString *) description;
@end
实现文件:Animal.m
#import "Animal.h"
@implementation Animal
- (instancetype)initWithWeight:(int)weight
{
if ([super init]) {
_weight = weight;
}
return self;
}
- (NSComparisonResult) compareWithWeight:(Animal *)another
{
if (self.weight<another.weight) {
return NSOrderedAscending;
}
if (self.weight>another.weight) {
return NSOrderedDescending;
}
else{
return NSOrderedSame;
}
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@: weight=%d", [super description], self.weight];
}
@end