//

//  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排序的更多相关文章

  1. 【转】NSArray排序方法

    原文网址:http://www.cnblogs.com/xiaobaizhu/archive/2013/06/05/3119983.html 从网上查的,非常方便的排序api,功能也很强大 1.sor ...

  2. NSArray排序方法讲解

    NSArray排序方法讲解 给数组排序有着多种方式 最麻烦的是sortedArrayUsingSelector:,其次是sortedArrayUsingDescriptors:,最容易使用的就是sor ...

  3. [OC Foundation框架 - 8] NSArray排序

    1.派生 voidarrayNew() { NSArray*array = [NSArrayarrayWithObjects:",nil]; NSArray*array2 = [arraya ...

  4. NSDictionary 和NSArray 排序(sort)

    排序: NSMutableDictionary *dic=[[NSMutableDictionary alloc]init]; [dic setValue:@"第3个" forKe ...

  5. NSArray 排序

    先研究一种方法 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:]; ; i < ; i++) { ; [arr ...

  6. ios 排序汇总

    ios 排序汇总  IOS几种简单有效的数组排序方法 //第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象 N ...

  7. 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词

    一. 字符串 API 1. NSString 用法简介 (1) NSString API 介绍 NSString 功能 : -- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 Stri ...

  8. oc72--NSArray排序

    // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomi ...

  9. NSArary自定义对象排序 NSComparator, compare

    reference from :http://mobile.51cto.com/hot-434804.htm 1.构建Person类 Person.h @interface Person : NSOb ...

随机推荐

  1. int左移32位的行为未定义/Coverity

    int左移32位的行为未定义 Coverity 代码静态安全检测 Is Shifting more than 32 bits of a uint64_t integer on an x86 machi ...

  2. 10. 求N分之一序列前N项和

    求N分之一序列前N项和 #include <stdio.h> int main() { int i, n; double item, sum; while (scanf("%d& ...

  3. 【翻译】Kinect v2程序设计(C++) BodyIndex篇

    通过Kinect SDK v2预览版,取得BodyIndex(人体区域)的方法和示例代码. 上一节,介绍了从Kinect v2预览版用Kinect SDK v2预览版获取Depth数据的方法.   这 ...

  4. php开源项目

    论坛社区:Discuz.PHPWind.ThinkSAAS.phpBB CMS内容管理:DedeCMS.PHPCMS.帝国CMS.齐博CMS.Drupal 企业建站:CmsEasy.KingCMS.P ...

  5. mysql查询昨天本周上周上月

    昨天 $yestoday = date("Y-m-d 00:00:00",strtotime('-1day'));$today = date("Y-m-d 00:00:0 ...

  6. java new synchronized

    java provides the synchronized keyword for synchronizing thread access to critical sections. Because ...

  7. Nginx return 关键字配置小技巧

    Nginx的return关键字属于HttpRewriteModule模块: 语法:return http状态码 默认值:无 上下文:server,location,if 该指令将结束执行直接返回htt ...

  8. 在MyEclipse下创建Java Web项目 入门(图文并茂)经典教程

    http://jijiaa12345.iteye.com/blog/1739754 在MyEclipse下创建Java Web项目 入门(图文并茂)经典教程 本文是一篇在Myeclipse下构建Jav ...

  9. openCV中IplImage的使用

    http://blog.csdn.net/welcome_xu/article/details/7650680 IplImage结构详细分析   IplImage 结构解读: typedef stru ...

  10. Mars 是微信官方的终端基础组件,是一个使用 C++ 编写的业平台性无关的基础组件

    http://www.oschina.net/p/wechat-mars http://www.oschina.net/news/80453/wewechat-open-source-plan