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方法的默认实现能在一定程度上简化程序代码,并且增强实例变量的访问安全性 ...
随机推荐
- 关于正则表达式的“\b”
今天刚刚开始看正则表达式就遇到一个十分头疼的问题,原文是这样的: “不幸的是,很多单词里包含hi这两个连续的字符,比如him,history,high等等.用hi来查找的话,这里边的hi也会被找出来. ...
- [转]JSP页面的动态包含和静态包含示例及介绍
原文地址:http://www.jb51.net/article/53659.htm 一.静态包含 本文介绍JSP静态包含语句,即使用JSP的include指令来完成的包含操作.JSP中,有两种包含其 ...
- InstallShield脚本事件
脚本事件主要有三大类:Before Move Data(安装数据前).Move Data(安装数据过程中).After Move Data(安装数据后). OnBegin:在初始化后,被调用 OnFi ...
- python 全局变量的使用
我尝试使用 类似 C 语言的方式去调用 python 的全局变量,发现不行,后经过 尝试,要使用 global 进行调用 test_num = 0; # 首先声明一个全局变量 def test_fun ...
- 一场由SD卡引发的灾难_转
注:此文章转自“https://user.qzone.qq.com/63915185/blog/1512562541”. Flash里面的数据在使用过程中莫名改变或不翼而飞?程序丢失可能无法正 ...
- hive表增量抽取到oracle数据库的通用程序(一)
hive表增量抽取到oracle数据库的通用程序(二) sqoop在export的时候 只能通过--export-dir参数来指定hdfs的路径.而目前的需求是需要将hive中某个表中的多个分区记录一 ...
- android开发(27) 看看我的手机里都有什么传感器
想看看我的HTC ONE x 具有什么传感器.写个代码RUN一下. 代码很简单,直接贴了 package zyf.demo.sensordemo; import java.util.List; imp ...
- webpack2--webpack 4.X 快速创建demo
准备工作 1.新建文件夹:webpack-demo(下面我们简称该文件夹为根目录),在根目录下面建两个文件夹,分别为src和dist. 1).src文件夹:用来存放我们编写的javascript代码, ...
- Qt中与文件目录相关操作
一.与文件目录操作有关操作. Qt中与文件目录相关的操作在QDir中,需加入#include <QDir>语句. QDir::drives()是列出电脑根目录下的所有目录,返回的是QFil ...
- Linux之安装本地Python和pip
wget https://www.python.org/ftp/python/3.4.5/Python-3.4.5.tgz tar zxfv Python-3.4.5.tgz ./configure ...