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:(具体实现方法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@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 ,将第二步中的数组作为参数传递进去,获得的结果就是已经排序好的数组了。

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

转载:http://www.cnblogs.com/BeyondAverage0908/p/4571638.html

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

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

    //创建一个数组 NSArray *array = @[@"one", @"two", @"three", @"four" ...

  2. OC数组排序

    NSArray *array = @[@"tailong", @"kaersasi", @"airuiliya", @"yingl ...

  3. iOS 开发小常识 开发笔记

    一   自定义push方法 /*  参数说明 *  controllerName : push的目标页 例:@“testcontroll”    ---注意不带.h *  isNibPage     ...

  4. 数组NSArray与NSMutableArray的常用方法

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

  5. iOS学习16之OC集合遍历和数组排序

    1.集合遍历 1> 遍历 集合(Collection):OC中提供的容器类:数组,字典,集合. 遍历:对集合中元素依次取出的过称叫做遍历. 三种方式:① for循环遍历: ② NSEnumera ...

  6. OC中数组排序的3种方法

    总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors: 大体上 ...

  7. NSSortDescriptor对象进行数组排序

    //创建一个数组 NSArray *array = @[@"zhangsan", @"lisi", @"zhonger", @"z ...

  8. OC:Block语法、Block使用、Block实现数组排序

    Block //定义一个求两个数最大值函数 int maxValue (int ,int); //函数的实现 int maxValue (int a, int b){ return  a > b ...

  9. NSSortDescriptor(数组排序)

    如果数组里面的每一个元素都是一个个model,例如 DepartsDate.h文件 [plain] view plaincopy #import <Foundation/Foundation.h ...

  10. OC中数组排序总结

    过完节回来,感觉很多东西都生疏了.总结一下数组的排序.应该不会太完美,后续添加补充. OC中的数组排序方法其实不太多,要根据不同的使用场景来使用不同的方法.Foundation框架中一般用到一下几个方 ...

随机推荐

  1. ACM 疯牛

    疯牛 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...

  2. web安全测试

  3. 【HDU】4089 Activation

    http://acm.hdu.edu.cn/showproblem.php?pid=4089 题意: 有n个人排队等着在官网上激活游戏.主角排在第m个. 对于队列中的第一个人.有以下情况:1.激活失败 ...

  4. poj 1847 最短路简单题,dijkstra

    1.poj  1847  Tram   最短路 2.总结:用dijkstra做的,算出a到其它各个点要改向的次数.其它应该也可以. 题意: 有点难懂.n个结点,每个点可通向ki个相邻点,默认指向第一个 ...

  5. JAVA生成带Logo的二维码

    1.下载生成二维码所需要的jar包qrcode.jar: 2.直接上生成二维码的java代码 //需要导入的包 import java.awt.Color;import java.awt.Graphi ...

  6. Weka使用介绍

    (转) http://baidutech.blog.51cto.com/4114344/1033714/ 1.简介 数据挖掘.机器学习这些字眼,在一些人看来,是门槛很高的东西.诚然,如果做算法实现甚至 ...

  7. 使用数据泵导入(impdp)和导出(expdp)

    数据泵技术是Oracle Database 10g 中的新技术,它比原来导入/导出(imp,exp)技术快15-45倍.速度的提高源于使用了并行技术来读写导出转储文件. expdp使用 使用EXPDP ...

  8. Mysql bench执行sql语句批量操作数据所遇到的问题

    一.错误 rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE ...

  9. Hadoop.2.x_时间服务器搭建(CentOs6.6)

    一.检查linux系统NTP是否被安装 [liuwl@hadoop09-linux-01 ~]$ [liuwl@hadoop09-linux-01 ~]$ rpm -qa | grep ntp ntp ...

  10. Oracle登录操作系统验证和密码文件验证

    1.确认数据库版本 2.查看当前配置文件 ORALCE数据库不同的登录验证方式是和SQLNET.ORA配置文件有关系的,在配置文件中有一个参数sqlnet.authentication_service ...