NSSortDescriptor 的使用

时间:2023-03-09 16:31:05
NSSortDescriptor 的使用

NSSortDescriptor  是什么 ?

你可以将它看做是对一个排序规则的描述者  因为我们可以使用它来对我们数组中的对象进行排序操作

假设现在有这样一个需求: 数组里面有十个Person对象 每一个Person对象有自己的名字年龄和分数  我们需要对数组里面的这十个对象做排序操作 规则如下:

1.首先按照年龄排序

2.如果年龄相同按照分数排序

要实现这个需求 如果你之前 不知道NSSortDescriptor 这个对象 你可能会写很多的代码 现在借助这个对象我们会非常容易的实现上面的需求

下面是实例:

首先我们需要创建一个Person类: 包括姓名 年龄 分数三个属性  同时提供一个构造方法 用于快速创建一个Person对象 代码如下:

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property(nonatomic,strong)NSString *name;

@property(nonatomic,assign)NSInteger age;

@property(nonatomic,assign)float  score;

- (instancetype)initWithName:(NSString*)name age:(NSInteger)age score:(float)score;

@end
#import "Person.h"

@implementation Person

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age score:(float)score
{
if (self = [super init]) { self.name = name;
self.age = age;
self.score = score; }
return self;
} @end

接下来我们在 ViewController(ViewController继承自UITableviewController 并且程序的跟控制器是一个导航控制器)然后做如下的几件事情:

1. 创建十个Person对象 并且存入datas数组中

2.将数据用TableView展示

3.设置导航栏左边的按钮为排序 点击排序 可以按照我们设定的规则进行排序

创建十个Person对象 并且存入datas数组中 我们给ViewController 增加一个数组属性 datas 强引用着

@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *datas;
@end

然后对datas采取懒加载的方式:

- (NSMutableArray *)datas
{
if (!_datas) { _datas = [NSMutableArray array]; Person *p1 = [[Person alloc] initWithName:@"jack" age: score:];
Person *p2 = [[Person alloc] initWithName:@"anne" age: score:];
Person *p3 = [[Person alloc] initWithName:@"zhng" age: score:];
Person *p4 = [[Person alloc] initWithName:@"tuoma" age: score:];
Person *p5 = [[Person alloc] initWithName:@"gril" age: score:];
Person *p6 = [[Person alloc] initWithName:@"boy" age: score:];
Person *p7 = [[Person alloc] initWithName:@"big" age: score:];
Person *p8 = [[Person alloc] initWithName:@"hack" age: score:];
Person *p9 = [[Person alloc] initWithName:@"zoom" age: score:];
Person *p10 = [[Person alloc] initWithName:@"right" age: score:]; [_datas addObject:p1];
[_datas addObject:p2];
[_datas addObject:p3];
[_datas addObject:p4];
[_datas addObject:p5];
[_datas addObject:p6];
[_datas addObject:p7];
[_datas addObject:p8];
[_datas addObject:p9];
[_datas addObject:p10]; }
return _datas;
}

接下来我们先把这些数据展示出来 在ViewController里面写上如下代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.datas.count;
} - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
} Person *p = self.datas[indexPath.row]; cell.textLabel.text = [NSString stringWithFormat:@"姓名:%@ 年龄:%zd",p.name,p.age];
cell.detailTextLabel.text =[NSString stringWithFormat:@"%f", p.score]; return cell;
}

接下来我们可以运行一下看看界面效果:

NSSortDescriptor 的使用

接下来我们需要进行排序:

规则如下:

1.首先按照年龄排序

2.如果年龄相同按照分数排序

那么我们需要创建排序描述者,一个描述着只能对一个属性进行描述 如果需要描述多个 我们需要创建多个描述者

我们这里的需求就需要创建两个描述者  一个是对年龄描述 一个是对分数描述 代码如下:

NSSortDescriptor *ageSD = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];//ascending:YES 代表升序 如果为NO 代表降序
NSSortDescriptor *scoreSD=[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];

创建好这两个描述者之后 我们就可以调用数组的 sortedArrayUsingDescriptors 方法来实现排序

sortedArrayUsingDescriptors方法接收一个数组的参数 里面放描述者 然后他会返回一个排序好的数组 所以我们这样做:

self.datas = [[self.datas sortedArrayUsingDescriptors:@[ageSD,scoreSD]] mutableCopy];

我们点击导航栏左边的排序按钮的时候 会执行以下操作:

- (IBAction)sortAge:(id)sender {

    NSSortDescriptor *ageSD = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
NSSortDescriptor *scoreSD=[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES]; self.datas = [[self.datas sortedArrayUsingDescriptors:@[ageSD,scoreSD]] mutableCopy]; [self.tableView reloadData]; }

这个时候 我们再看下效果:

NSSortDescriptor 的使用

可以看到 如果年龄相同 我们就按照分数进行排序  这样我们的这个排序就完成了 如果你有多个排序需求 你就创建多个排序描述者就可以了 是不是很简单