OC-数组排序-NSSortDescriptor使用
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使用的更多相关文章
- OC中用NSSortDescriptor对象进行数组排序
//创建一个数组 NSArray *array = @[@"one", @"two", @"three", @"four" ...
- OC数组排序
NSArray *array = @[@"tailong", @"kaersasi", @"airuiliya", @"yingl ...
- iOS 开发小常识 开发笔记
一 自定义push方法 /* 参数说明 * controllerName : push的目标页 例:@“testcontroll” ---注意不带.h * isNibPage ...
- 数组NSArray与NSMutableArray的常用方法
数组中可以放任何类型的数据,并且一个数组中的元素类型可以不一致.只要是(id类型)对象. NSArray 1.初始化 NSArray *array = @[]; 2.初始化,最后需要以nil结尾 NS ...
- iOS学习16之OC集合遍历和数组排序
1.集合遍历 1> 遍历 集合(Collection):OC中提供的容器类:数组,字典,集合. 遍历:对集合中元素依次取出的过称叫做遍历. 三种方式:① for循环遍历: ② NSEnumera ...
- OC中数组排序的3种方法
总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors: 大体上 ...
- NSSortDescriptor对象进行数组排序
//创建一个数组 NSArray *array = @[@"zhangsan", @"lisi", @"zhonger", @"z ...
- OC:Block语法、Block使用、Block实现数组排序
Block //定义一个求两个数最大值函数 int maxValue (int ,int); //函数的实现 int maxValue (int a, int b){ return a > b ...
- NSSortDescriptor(数组排序)
如果数组里面的每一个元素都是一个个model,例如 DepartsDate.h文件 [plain] view plaincopy #import <Foundation/Foundation.h ...
- OC中数组排序总结
过完节回来,感觉很多东西都生疏了.总结一下数组的排序.应该不会太完美,后续添加补充. OC中的数组排序方法其实不太多,要根据不同的使用场景来使用不同的方法.Foundation框架中一般用到一下几个方 ...
随机推荐
- What is classical music
quanben's definition of classical music is a definition formed by the following aspects, 1. music wr ...
- ubuntu 安装Firefox 29.0
下载Firefox 29.0 % cd ~/Downloads % sudo cp firefox-29.0.tar.bz2 /opt % cd /opt % sudo tar -xvjf firef ...
- 【CF】438E. The Child and Binary Tree
http://codeforces.com/contest/438/problem/E 题意:询问每个点权值在 $c_1, c_2, ..., c_m$ 中,总权值和为 $s$ 的二叉树个数.请给出每 ...
- 再说virtual
看了对Anders Hejlsberg的采访, 1)C#中函数默认是非virtual的设计因为:在java中,一个方法默认是虚拟化的,只有对一个方法必须声明final关键字,这样这个方法才是非虚的,无 ...
- javascript 时间操作
javascript时间函数 javascript提供了Date对象来进行时间和日期的计算.Date对象有多种构造函数: 1.dateObj=new Date() //当前时间 2.dateObj=n ...
- jquery ajax传递多个对象或数组到后台
1.js对象创建:因为需要把对象json序列化后,才能传递到后台,后台根据json字符串进行反序列化. 2.Jquery $.ajax方法的配置 针对$.ajax方法的配置参数需要进行修改: 1) ...
- github标记
<template> <a href="https://github.com/lmk123/Runner" class="github-corner&q ...
- python操作Excel读写--使用xlrd和xlwt
一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 进入到解压文件路径,输入 setup.py ...
- 网站部署后Parser Error Message: Could not load type 的解决方案
asp.net 的Webproject 项目是在64bit机上开发,默认选项发布后,部署到32bit的服务器上,出现Parser Error Message: Could not load type的 ...
- 禁止北京地区IP访问站点
<script type="text/javascript" src="http://counter.sina.com.cn/ip" charset=&q ...