OC-数组排序-NSSortDescriptor使用

在Object-c中,当有一个班级类MyClass,还有一个学生类Student.在班级类MyClass中通过一个可变数组NSMutableArray保存了许多的学生对象。现在有一个功能需要对学生进行按各种条件排序(按姓名升序,按学号降序,按成绩升序)。

借用上面一个简单的功能来了解一下Object-c中排序描述对象NSSortDescriptor对数组的排序的使用方法。

简单的了解一下以下几个类:Student(学生类)、MyClass(班级类)

Student:(只给接口)

 1 @interface Student : NSObject
 2 {
 3     NSString * _name;
 4     NSInteger _num;
 5     NSInteger _score;
 6 }
 7
 8 - (id)initWithName:(NSString *)name number:(NSInteger)num score:(NSInteger)score;
 9 - (void)setName:(NSString *)name;
10 - (NSString *)name;
11 - (void)setNum:(NSInteger)num;
12 - (NSInteger)num;
13 - (void)setScore:(NSInteger)score;
14 - (NSInteger)score;
15
16 @end

MyClass:(接口)

@interface MyClass : NSObject
{
    NSString * _className;
    NSMutableArray * _stuList;
}

- (id)init;
- (void)setClassName:(NSString *)name;
- (NSString*)className;
- (void)addStudent:(Student *)student;
- (void)addStudent:(Student *)student atIndex:(NSInteger) index;
- (void)removeStudent:(Student *)student;
- (void)removeStudentAtIndex:(NSInteger) index;
- (void)replaceStudent:(Student *)student atIndex:(NSInteger) index;
- (void)showStuList;
- (void)sortedByNumber;//按照学号升序
- (void)sortedByScore;//按照分数降序
- (void)sortedByName;//按照名字降序

- (void)sortedByNameAscByNumDescByScoreAsc;
@end

MyClass:(具体实现方法)

@implementation MyClass
- (void)sortedByNumber  //按照学号升序
{
    NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:@"_num" ascending:YES];
    NSArray *sdArray = [NSArray arrayWithObjects:sd1, nil];
    [_stuList sortUsingDescriptors:sdArray];
//    [_stuList sortedArrayUsingDescriptors:sdArray];
}
- (void)sortedByScore    //按照分数降序
{
    NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"_score" ascending:NO];
    NSArray *sdArray = [[NSArray alloc]initWithObjects:sd, nil];
    [_stuList sortUsingDescriptors:sdArray];
}
- (void)sortedByName   //按照名字降序
{
    NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"_name" ascending:NO];
    NSArray *sdArray = [[NSArray array] initWithObjects:sd, nil];
    [_stuList sortUsingDescriptors:sdArray];
}
- (void)sortedByNameAscByNumDescByScoreAsc //按照名字升序,学号降序,分数升序来排列数组中的对象
{
    NSSortDescriptor *sdName = [[NSSortDescriptor alloc]initWithKey:@"_name" ascending:YES];
    NSSortDescriptor *sdNum = [[NSSortDescriptor alloc]initWithKey:@"_num" ascending:NO];
    NSSortDescriptor *sdScore = [NSSortDescriptor sortDescriptorWithKey:@"_score" ascending:YES];
    NSArray *sdArray = [[NSArray alloc] initWithObjects:sdName,sdNum,sdScore, nil];
    [_stuList sortUsingDescriptors:sdArray];
}
@end

就由上面的 sortedByNameAscByNumDescByScoreAsc 方法来分析NSSortDescriptor对象的使用方法。

1.通过NSSortDescriptor对象的对象方法 initWithKey 或是直接通过类方法  sortDescriptorWithKey 生成一个NSSortDescriptor(排序描述)对象,该对象传进一个排序关键字(该关键字是要排序的数组中元素对象的属性),并且设置按照该关键字(属性)是按照升序还是降序。

2.将NSSortDescriptor对象(可以多个)添加到一个数组中。

3.最后通过数组的方法 sortUsingDescriptors ,将第二步中的数组作为参数传递进去,获得的结果就是已经排序好的数组了。

以上方法适用于可变数组和不可变数组,只是方法略微有点不一样。

时间: 2024-08-30 04:46:31

OC-数组排序-NSSortDescriptor使用的相关文章

OC中用NSSortDescriptor对象进行数组排序

//创建一个数组 NSArray *array = @[@"one", @"two", @"three", @"four", @"six"]; //创建一个排序条件,也就是一个NSSortDescriptor对象 //其中第一个参数为数组中对象要按照什么属性来排序(比如自身.姓名,年龄等) //第二个参数为指定排序方式是升序还是降序 //ascending 排序的意思,默认为YES 升序 NSSortDes

OC 类型转换

NSUInteger转换为NSNumber可以通过 NSNumber *aNumber = [NSNumber numberWithInteger:21]; 来转换,然后把NSNumber放到数组里 最后要用数据的时候,把NSNumber从数组里取出来,然后通过 NSInteger anInteger = [aNumber integerValue]; 拿到那个数字21 数组排序 NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWi

NSArray与NSMutableArray的常用方法

数组中可以放任何类型的数据,并且一个数组中的元素类型可以不一致.只要是(id类型)对象. NSArray 1.初始化 NSArray *array = @[@"123", @"456", @2]; 2.初始化,最后需要以nil结尾 NSArray *array = [[NSArray alloc] initWithObjects:@"123", @"456", nil]; 3.初始化,使用一个数组初始化另一个数组 NSArra

NSSortDescriptor对象进行数组排序

//创建一个数组 NSArray *array = @[@"zhangsan", @"lisi", @"zhonger", @"zhubada", @"honghunag"]; //创建一个排序条件,也就是一个NSSortDescriptor对象 //其中第一个参数为数组中对象要按照什么属性来排序(比如自身.姓名,年龄等) //第二个参数为指定排序方式是升序还是降序 //ascending  排序的意思,默

OC第六节 遍历集合、数组排序

1.遍历的?式?for.枚举器.forin. ①.for循环同C语言,循环条件是集合的元素个数或键值对个数,无序的集合要先转换成数组在根据下标遍历 ②.枚举器 NSEnumerator objectEnumerator (reverseObjectEnumerator)   创建枚举器(反向枚举器) nextObject  与while循环连用,遍历集合 ③. for … in :快速枚举,是在NSEnumerator的基础上封装的更加方便的快熟便立即和元素的方式,格式如下: for (<#ty

OC中Block语法、Block使用以及通过Block实现数组排序

Block:语法块,本质上是匿名函数(没有名称的函数) 标准C里面没有Block,C语言的后期扩展版本,加入了匿名函数 在C++.JS.Swift等语言有类似语法,叫做闭包 Block语法和C语言里的函数指针很相似,下面我们先来回顾下C语言中的函数和函数指针 C语言中函数是指:实现某一功能的代码段 完整的函数包括两部分:函数声明.函数定义 函数声明,即函数原型.例如:int sum (int x,int y);具有两个整形参数,一个整形返回值的函数 函数定义,即函数实现.例如:int sum(i

ObjectC----字典类和集合类以及快速枚举和OC中的数组排序

// Create By 郭仔  2015年04月01日20:06:36 // 不可变字典 // 字典是适用于存放键值对的一种集合,里面的元素必须是对象类型 // 字典是无序的 // 字典赋值 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"guozai",@"name",@"nan",@"sex",@"14",@"

OC --(5)-- 字典、集、数组排序:字典类、集合类、数组数组排序、字典、集合的快速遍历、数组排序

字典 1.快速枚举 forin   运行时不能更改内部数据 for (<#type *object#> in <#collection#>) { <#statements#> }         //对象                    //数组 for (Contact *contact in friends) {  [contact show];  } 判定是否为空  NSString *ns ==nil      NSInteger inte==0 一.字典

OC对数组排序的方法

方法一: 使用sortUsingSelector 如: [_cards sortUsingSelector:@selector(isSortByContactName:)]; [_cards sortUsingSelector:@selector(isSortBYPhoneNumber:)]; -(BOOL)isSortByContactName:(Card *)aCard{    if ([_contactName compare:[aCard contactName]] ==NSOrdere

OC --(6)-- Block、数组高级:Block语法、Block使用、Block实现数组排序

1.Block定义 Block:块语法,本质上是匿名函数(没有名称的函数) 标准C?里?面没有Block,C语言的后期扩展版本,加入了匿名函 数. C++.JS.Swift等语?言,有类似语法,叫做闭包. Block语法和函数指针很相似.