Objective-C语法之NSSortDescriptor
main.m
#import <Foundation/Foundation.h>
#import "Person.h"
/**
NSSortDescriptor 可以实现按照对象的属性进行排序;支持多个属性排序。比如我们有个Person对象,它有名字(name)和年龄(age)两个属性,我们需要按Person的age属性(降序)和name属性(升序)来输出Person对象数组信息
*/
void testSortDescriptor() {
NSMutableArray *mArrPerson = [[NSMutableArray alloc] initWithCapacity:];
Person *p = [[Person alloc] initWithName:@"KK" age:];
[mArrPerson addObject:p];
p = [[Person alloc] initWithName:@"Candy" age:];
[mArrPerson addObject:p];
p = [[Person alloc] initWithName:@"Wiky" age:];
[mArrPerson addObject:p];
p = [[Person alloc] initWithName:@"Stone" age:];
[mArrPerson addObject:p];
p = [[Person alloc] initWithName:@"Tom" age:];
[mArrPerson addObject:p];
p = [[Person alloc] initWithName:@"Sherlock" age:];
[mArrPerson addObject:p];
p = [[Person alloc] initWithName:@"Alex" age:];
[mArrPerson addObject:p];
p = [[Person alloc] initWithName:@"Keye" age:];
[mArrPerson addObject:p]; NSLog(@"按Person的age属性(降序)和name属性(升序)");
NSSortDescriptor *sortByAge = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO];
NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[mArrPerson sortUsingDescriptors:@[sortByAge, sortByName]];
for (Person *p in mArrPerson) {
NSLog(@"age=%ld, name=%@", p.age, p.name);
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
testSortDescriptor();
}
return ;
}
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age; - (id)initWithName:(NSString *)name age:(NSInteger)age;
@end
Person.m
#import "Person.h" @implementation Person
- (id)initWithName:(NSString *)name age:(NSInteger)age {
if (self = [super init]) {
_name = name;
_age = age;
}
return self;
}
@end
结果:
-- ::13.240 OCNSSortDescriptor[:] 按Person的age属性(降序)和name属性(升序)
-- ::13.241 OCNSSortDescriptor[:] age=, name=Stone
-- ::13.241 OCNSSortDescriptor[:] age=, name=Alex
-- ::13.241 OCNSSortDescriptor[:] age=, name=Keye
-- ::13.241 OCNSSortDescriptor[:] age=, name=Tom
-- ::13.242 OCNSSortDescriptor[:] age=, name=Sherlock
-- ::13.242 OCNSSortDescriptor[:] age=, name=Wiky
-- ::13.242 OCNSSortDescriptor[:] age=, name=KK
-- ::13.242 OCNSSortDescriptor[:] age=, name=Candy
Objective-C语法之NSSortDescriptor的更多相关文章
- 初学Objective - C语法之代码块(block)
一.block声明 1.无参数,无返回值: void (^sayHi)(); 2.有参数,有返回值: NSInteger (^operateOfValue)(NSInteger num); block ...
- [转] 从 C 到 Objective C 入门1
转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...
- iOS开发——语法篇OC篇&高级语法精讲二
Objective高级语法精讲二 Objective-C是基于C语言加入了面向对象特性和消息转发机制的动态语言,这意味着它不仅需要一个编译器,还需要Runtime系统来动态创建类和对象,进行消息发送和 ...
- Windows下编译objective-C
Windows下编译objective-C 2011-08-31 14:32 630人阅读 评论(0) 收藏 举报 windowscocoa工具objective clibraryxcode 目录 ...
- ios学习笔记之2天来总结
学了2天,小结下. ios的基本代码执行流程: 与java的基本异同: 异: 1.基类:java中Object是所有类的父类,而objective-c的根类为NSObject 2.默认访问类型:jav ...
- Pentaho BI server 中 CCC table Component 的使用小技巧
我使用的版本 Pentaho BI Server 5.3.0.0.213 CDE/CDF/CDA/CCC 15.04.16 stable Q: 如何设置表格中各种提示文字的语言(默认为英语)? C ...
- OC中protocol、category和继承的关系--转
开放封闭原则(OCP)就是,“对扩展开放,对更改封闭”.是所有面向对象设计的一个核心宗旨.感兴趣的可以看百度百科的一些解释:http://baike.baidu.com/view/2493421.ht ...
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- Objective - C中属性和点语法的使用
一.属性 属性是Objective—C 2.0定义的语法,为实例变量提供了setter.getter方法的默认实现能在一定程度上简化程序代码,并且增强实例变量的访问安全性 ...
随机推荐
- burpsuite两个变量的爱情故事
抓包的时候在攻击类型处选择[Cluster bomb] 在payload type这里设置类型为[simple list] 第一个是账号 第二个是密码 分批加载即可
- jquery文字纵向滚动效果(带间隔停留)
<script type="text/javascript"> //文字纵向滚动 $(function() { var $this = $("#quotati ...
- CentOS6.3升级Python到2.7.3版本
http://www.zhangchun.org/the-centos6-3-upgrade-python-to-2-7-3-version/ 查看python的版本 1 python -V 2 ...
- C语言 · 最长字符串
算法训练 最长字符串 时间限制:1.0s 内存限制:512.0MB 求出5个字符串中最长的字符串.每个字符串长度在100以内,且全为小写字母. 样例输入 one two three ...
- Maven MyEclipse创建web项目没有src/maim/java
转载:http://blog.csdn.net/nich002/article/details/43273219 maven项目 错误: 找不到或无法加载主类 分类: java2015-01-29 ...
- IIS 日志分析
查看哪个IP访问量大,访问了什么地址,大可以看到攻击者IP: select c-ip,count(c-ip) AS allcount,cs-uri-stem,cs-uri-query,cs(User- ...
- NHibernate 知识点整理
1.实体bool类型属性与数据库映射.如实体中有bool属性IsDelete.配置如下 <property column="IsDelete" name="IsDe ...
- WmS具体解释(二)之怎样理解Window和窗体的关系?基于Android7.0源代码
上篇博客(WmS具体解释(一)之token究竟是什么?基于Android7.0源代码)中我们简要介绍了token的作用,这里涉及到的概念非常多,当中出现频率最高的要数Window和窗体这一对搭档了,那 ...
- jQuery(三):样式操作
一.DOM操作分类 DOM Core:任何一种支持DOM的编程语言都可以使用它,例如:getElementById(). HTML-DOM:用于处理HTML文档,例如:document.forms. ...
- MFC函数—— CFrameWnd::OnCreateClient
CFrameWnd::OnCreateClient virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs,CCreateContext*pContext); ...