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方法的默认实现能在一定程度上简化程序代码,并且增强实例变量的访问安全性 ...
随机推荐
- svn 版本管理与自动部分发布
作为团队开发项目时,会遇到项目的版本管理,测试部署与发布部署,下面是摘至他人的关于版本管理和自动部署的方案. svn自动部署的实现: 使用svn的hook功能 1.在版本库的hooks目录下面,有一些 ...
- iOS贝塞尔曲线(UIBezierPath)的基本使用方法
简介 UIBezierPath是对Core Graphics框架的一个封装,使用UIBezierPath类我们可以画出圆形(弧线)或者多边形(比如:矩形)等形状,所以在画复杂图形的时候会经常用到. 分 ...
- python dict conver json
demo: import jsonimport requestsimport xml.etree.ElementTree as et def xmlsjondemo(): data={'statusc ...
- python keras 神经网络框架 的使用以及实例
先吐槽一下这个基于theano的keras有多难装,反正我是在windows下折腾到不行(需要64bit,vs c++2015),所以自己装了一个双系统.这才感到linux系统的强大之初,难怪大公司都 ...
- Java Web项目中使用Freemarker生成Word文档
Web项目中生成Word文档的操作屡见不鲜.基于Java的解决方式也是非常多的,包含使用Jacob.Apache POI.Java2Word.iText等各种方式,事实上在从Office 2003開始 ...
- 越狱机器SSH安装与使用
SSH安装html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scroll { ...
- 在Word 2007中添加参考文献及其引用的方法
以前写文章的时候忽略了在文章中添加参考文献及其引用的方式,文章各式显得不太正式,在网上进行了相关搜索,将方法整理如下: 1.将光标停留在需要插入文献的地方[1],选择菜单栏上的"引用 -&g ...
- JBoss Wildfly (1) —— 7.2.0.Final编译
JBoss Wildfly (1) -- 7.2.0.Final编译 wildfly版本: 7.2.0.Final-testsuite-fix jdk版本: jdk1.7.0_79 maven版本: ...
- selenium(java)处理HTML5的视频播放
大多数浏览器使用控件(如 Flash) 来播放规频,但是,不同的浏览器需要使用不同的插件.HTML5 定义了一个新的元素<video>,,指定了一个标准的方式来嵌入电影片段.IE9+.Fi ...
- Mxnet 查看模型params的网络结构
import mxnet as mx import pdb def load_checkpoint(): """ Load model checkpoint from f ...