nsarray排序
//
// main.m
// 05-数组排序
//
// Created by apple on 14-3-21.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//1.使用sortedArrayUsingSelector
//也是最简单的排序方式
//数组是按照你存入元素的顺序存储的
NSArray * array = @[@"b",@"d",@"a",@"z"];
NSLog(@"排序前 array %@",array);
array = [array sortedArrayUsingSelector:@selector(compare:)];
// NSArray * array1 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后 array %@",array);
//2.使用block方式排序
NSArray * array2 = @[@"z",@"4",@"b",@"3",@"x"];
NSLog(@"array2 排序前 %@",array2);
array2 = [array2 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
/*
NSComparisonResult retuslt = [obj1 compare:obj2];
return retuslt;
*/
//obj1 compare obj2 就是正序排序
// return [obj1 compare:obj2];
//obj2 compare obj1 就是倒序排序
return [obj2 compare:obj1];
}];
NSLog(@"array2 排序后 %@",array2);
Person * p1 = [[Person alloc] initWithName:@"xiaozhe" andAge:20 andYear:@"1990"];
Person * p2 = [[Person alloc] initWithName:@"alex" andAge:18 andYear:@"2990"];
Person * p3 = [[Person alloc] initWithName:@"merry" andAge:25 andYear:@"1890"];
NSArray * array3 = @[p1,p2,p3];
NSLog(@"array3 排序前 %@",array3);
//3.使用 给数组排序
//如果你向给你自己定义的对象排序,必须根据某一个属性来排序,
//sortDescriptorWithKey 参数要的就是你对象中,要依据哪个属性来排序,你就把哪个属性的名字当成key传入
//ascending YES表示正序 NO表示倒叙
NSSortDescriptor * d1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
NSSortDescriptor * d2 = [NSSortDescriptor sortDescriptorWithKey:@"year" ascending:NO];
//如果你要使用多个属性进行排序,默认在前面的NSSortDescriptor优先级比较高
NSArray * descripts = @[d2,d1];
array3 = [array3 sortedArrayUsingDescriptors:descripts];
NSLog(@"array 3 排序后 %@",array3);
//4.
NSArray * array4 = @[p1,p2,p3];
NSLog(@"array4 排序前 %@",array4);
array4 = [array4 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Person * p1 = obj1;
Person * p2 = obj2;
//year
return [p1.year compare:p2.year];
}];
NSLog(@"array4 排序后 %@",array4);
}
return 0;
}
nsarray排序的更多相关文章
- 【转】NSArray排序方法
原文网址:http://www.cnblogs.com/xiaobaizhu/archive/2013/06/05/3119983.html 从网上查的,非常方便的排序api,功能也很强大 1.sor ...
- NSArray排序方法讲解
NSArray排序方法讲解 给数组排序有着多种方式 最麻烦的是sortedArrayUsingSelector:,其次是sortedArrayUsingDescriptors:,最容易使用的就是sor ...
- [OC Foundation框架 - 8] NSArray排序
1.派生 voidarrayNew() { NSArray*array = [NSArrayarrayWithObjects:",nil]; NSArray*array2 = [arraya ...
- NSDictionary 和NSArray 排序(sort)
排序: NSMutableDictionary *dic=[[NSMutableDictionary alloc]init]; [dic setValue:@"第3个" forKe ...
- NSArray 排序
先研究一种方法 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:]; ; i < ; i++) { ; [arr ...
- ios 排序汇总
ios 排序汇总 IOS几种简单有效的数组排序方法 //第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象 N ...
- 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词
一. 字符串 API 1. NSString 用法简介 (1) NSString API 介绍 NSString 功能 : -- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 Stri ...
- oc72--NSArray排序
// Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomi ...
- NSArary自定义对象排序 NSComparator, compare
reference from :http://mobile.51cto.com/hot-434804.htm 1.构建Person类 Person.h @interface Person : NSOb ...
随机推荐
- JavaScript系列:event.bubbles属性(并不是所有的事件都具有冒泡)
地址 https://www.w3.org/TR/DOM-Level-3-Events/#h3_interface-Event https://segmentfault.com/q/101000000 ...
- Tesla-> Fermi (550Ti) -> Kepler(680) -> Maxwell (750Ti) -> Volta(was Pascal)
Pascal GPU Pascal (from French mathematician Blaise Pascal) is Maxwell successor. In this news, we l ...
- PHP 多个文件上传
关键函数: is_uploaded_file():用于判断指定的文件是否是通过 HTTP POST 上传的,如果是则返回 TRUE.用于防止潜在的攻击者对原本不能通过脚本交互的文件进行非法管理,这可以 ...
- PHP常用正则表达式汇总 [复制链接]
PHP常用正则表达式汇总 [复制链接] 上一主题下一主题 离线我是小猪头 法师 发帖 539 加关注 发消息 只看楼主 倒序阅读 使用道具楼主 发表于: 2011-06-22 更多 ...
- oracle utf8字符集转gbk(转)
近日有同事在外面部署系统时,安装数据库时可能选择了UTF-8编码格式,导入insert语句时,一个汉字被认为三个字节,这是不行的. 结合上网搜到的资料,将oracle数据库的编码格式,从utf-8改为 ...
- 【转】如何使php的MD5与C#的MD5一致?
有c#生成MD5的代码如下: class CreateMD5 { static void Main(string[] args) { string source = "提问指南"; ...
- bug
expected identifier,string or number //这种问题一般是json数据中最后一个逗号没去掉.
- FTS抓包看蓝牙的SDP整个过程
1.概述 SDP是蓝牙的Service Discovery Protocol,用来发现远程设备能够提供的Service.它只负责发现对方支持的Service,不负责Service的具体实现. ...
- A2DP协议笔记
1.概述 A2DP(Advanced Audio Distribution Profile)是蓝牙的音频传输协议,典型应用为蓝牙耳机.A2DP协议的音频数据在ACL Link上传输,这与SCO ...
- 【nodejs】使用Node.js实现REST Client调用REST API
最近在产品中开发基于REST的API接口,结合自己最近对Node.js的研究,想基于它开发一个REST Client做测试之用. 通过初步研究,Node.js开发HTTP Client还是挺方便的. ...