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. Angular JS中的依赖注入

    依赖注入DI angularjs中与DI相关有angular.module().angular.injector(). $injector.$provide. DI 容器3要素:服务的注册.依赖关系的 ...

  2. DOM的概念及子节点类型

    前言 DOM的作用是将网页转为一个javascript对象,从而可以使用javascript对网页进行各种操作(比如增删内容).浏览器会根据DOM模型,将HTML文档解析成一系列的节点,再由这些节点组 ...

  3. doPost方法与doGet方法

    例子我们发现forward跳转访问Servlet说不定的感觉,其实我们要想弄明白这个问题,就要从forward本身来研究了. 我们都知道 forward跳转是转发请求,不转发地址的,简单点说,forw ...

  4. Codeforces Round #228 (Div. 2) A. Fox and Number Game

    #include <iostream> #include <algorithm> #include <vector> #include <numeric> ...

  5. 【BZOJ】1225: [HNOI2001] 求正整数

    http://www.lydsy.com/JudgeOnline/problem.php?id=1225 题意:给一个数n,求一个最小的有n个约数的正整数.(n<=50000) #include ...

  6. POJ 1279 Art Gallery(半平面交)

    题目链接 回忆了一下,半平面交,整理了一下模版. #include <cstdio> #include <cstring> #include <string> #i ...

  7. Resharp最新破解方法

    ReSharper是一个JetBrains公司出品的著名的代码生成工具,其能帮助Microsoft Visual Studio成为一个更佳的IDE.它包括一系列丰富的能大大增加C#和Visual Ba ...

  8. osg 路径 动画 效果

    osg 路径 动画 效果 转自:http://blog.csdn.net/zhuyingqingfen/article/details/8248157 #include <osg/Group&g ...

  9. linux vi 删除多行的方法

    dd 删除一行 d$ 删除以当前字符开始的一行字符 ndd 删除以当前行开始的n行 dw 删除以当前字符开始的一个字 ndw 删除以当前字符开始的n个字 D 与d$同义 d) 删除到下一句的开始 d} ...

  10. 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...