OC数组排序

NSArray *array = @[@"tailong", @"kaersasi", @"airuiliya", @"yingliuzhizhu"];

NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"genie", @"weizhuang", @"tianming", @"shaoyu", @"gaoyue", nil];

/**

*  1.使用NSSortDescriptor(排序描述符,相当于排序条件)

排序描述符由两个参数组成

key:对于一个给定的集合,对应值的键位对集合中的每个元素进行排序

accending:升序(YES)或降序(NO)

*/

// 给排序的数组生成排序描述符

NSSortDescriptor *arraySortDes = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];

// 不可变数组排序

NSArray *array1 = [array sortedArrayUsingDescriptors:@[arraySortDes]];

//NSLog(@"%@", array1);

for (NSString *obj in array1) {

NSLog(@"%@", obj);

}

// 可变数组排序

[mArray sortUsingDescriptors:@[arraySortDes]];

for (NSString *str in mArray) {

NSLog(@"%@", str);

}

//新建一个Person类

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *gender;
@property (nonatomic, assign) NSInteger age; // 自定义初始化方法
- (instancetype)initWithName:(NSString *)name
andGender:(NSString *)gender
andAge:(NSInteger)age; // 遍历构造器
+ (instancetype)personWithName:(NSString *)name
andGender:(NSString *)gender
andAge:(NSInteger)age; // 比较两个人的姓名
- (NSComparisonResult)compareByName:(Person *)person; // 比较两个人的年龄
- (NSComparisonResult)compareByAge:(Person *)person; @end

Person.m

#import "Person.h"

@implementation Person

// 自定义初始化方法
- (instancetype)initWithName:(NSString *)name
andGender:(NSString *)gender
andAge:(NSInteger)age {
if (self = [super init]) {
self.name = name;
self.gender = gender;
self.age = age;
}
return self;
} // 遍历构造器
+ (instancetype)personWithName:(NSString *)name
andGender:(NSString *)gender
andAge:(NSInteger)age {
Person *p = [[Person alloc] initWithName:name andGender:gender andAge:age];
return p;
} // 重写description
- (NSString *)description {
return [NSString stringWithFormat:@"name = %@, gender = %@, age = %ld", self.name, _gender, _age];
} // 比较两个人的姓名(降序在[]前面加个-)
- (NSComparisonResult)compareByName:(Person *)person {
return [self.name compare:person.name];
} // 比较两个人的年龄(降序把>改为<)
- (NSComparisonResult)compareByAge:(Person *)person {
return self.age > person.age;
} @end

// 数组中存放自定义对象进行排序

Person *p1 = [Person personWithName:@"tailong" andGender:@"男" andAge:27];

Person *p2 = [Person personWithName:@"manwang" andGender:@"男" andAge:24];

Person *p3 = [Person personWithName:@"hanbing" andGender:@"女" andAge:20];

Person *p4 = [Person personWithName:@"airuiliya" andGender:@"女" andAge:21];

Person *p5 = [Person personWithName:@"jiansheng" andGender:@"男" andAge:23];

//不可变数组

NSArray *personArray = @[p1, p2, p3, p4, p5];

// 按照姓名进行排序

// 创建排序描述对象

NSSortDescriptor *sortDes1 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

NSArray *personArray1 = [personArray sortedArrayUsingDescriptors:@[sortDes1]];

NSLog(@"%@", personArray1);

// 按照年龄进行排序

NSSortDescriptor *sortDes2 = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO];

NSArray *personArray2 = [personArray sortedArrayUsingDescriptors:@[sortDes2]];

NSLog(@"%@", personArray2);

// 可变数组

NSMutableArray *mPersonArray = [@[p1, p2, p3, p4, p5] mutableCopy];

// 按照年龄进行排序

NSSortDescriptor *mSortDes1 = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];

[mPersonArray sortUsingDescriptors:@[mSortDes1]];

NSLog(@"%@", mPersonArray);

// 按照姓名进行排序

NSSortDescriptor *mSortDes2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];

[mPersonArray sortUsingDescriptors:@[mSortDes2]];

NSLog(@"%@", mPersonArray);

/**

*  2.使用数组中两个元素比较的方法名排序

*/

// 不可变数组

SEL sel = @selector(compare:);

array = [array sortedArrayUsingSelector:sel];

NSLog(@"%@", array);

// 可变数组

[mArray sortUsingSelector:@selector(compare:)];

NSLog(@"%@", mArray);

// 数组中存放自定义对象进行排序

// 不可变数组排序

// 按照姓名排序

personArray = [personArray sortedArrayUsingSelector:@selector(compareByName:)];

NSLog(@"%@", personArray);

// 按照年龄进行排序

personArray = [personArray sortedArrayUsingSelector:@selector(compareByAge:)];

NSLog(@"%@", personArray);

// 可变数组

// 按照姓名排序

[mPersonArray sortUsingSelector:@selector(compareByName:)];

NSLog(@"%@", mPersonArray);

// 按照年龄进行排序

[mPersonArray sortUsingSelector:@selector(compareByAge:)];

NSLog(@"%@", mPersonArray);

上一篇:浅谈react的初步试用


下一篇:通用双向链表的设计(参考Linux系统中的实现)