首页 > 其他 > 详细

NSSortDescriptor和sortedArrayUsingDescriptors

时间:2014-07-23 14:48:36      阅读:339      评论:0      收藏:0      [点我收藏+]

NSDictionary *_dic1=[NSDictionary dictionaryWithObjectsAndKeys:@"2030",@"year", @"1",@"month",nil];

    NSDictionary *_dic2=[NSDictionary dictionaryWithObjectsAndKeys:@"2010",@"year", @"2",@"month", nil];

    NSDictionary *_dic3=[NSDictionary dictionaryWithObjectsAndKeys:@"2050",@"year", @"3",@"month" ,nil];

    NSDictionary *_dic4=[NSDictionary dictionaryWithObjectsAndKeys:@"2014",@"year",  @"4",@"month",nil];

     NSDictionary *_dic5=[NSDictionary dictionaryWithObjectsAndKeys:@"2050",@"year",  @"4",@"month",nil];

    NSArray *_array=[NSArray arrayWithObjects:_dic1,_dic2,_dic3,_dic4,_dic5, nil];

    

    NSSortDescriptor *descripor=[NSSortDescriptor sortDescriptorWithKey:@"year" ascending:NO];

    NSSortDescriptor *descripor2=[NSSortDescriptor sortDescriptorWithKey:@"month" ascending:NO];

    [_array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descripor,descripor2,nil]];

    NSLog(@"%@",[_array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descripor,descripor2,nil]]); 

打印出来的结果为:

        {

        month = 4;

        year = 2050;

    },

        {

        month = 3;

        year = 2050;

    },

        {

        month = 1;

        year = 2030;

    },

        {

        month = 4;

        year = 2014;

    },

        {

        month = 2;

        year = 2010;

    }

)  

对数组的元素是字典的情况进行排序

sortedArrayUsingDescriptors:(NSArray *)sortDescriptors

这里的NSArray中的第一元素表示首先按照这个元素的升序或者降序进行排序,对于有重复项的,再按照第二元素进行排序,依次进行类推

NSSortDescriptor和sortedArrayUsingDescriptors,布布扣,bubuko.com

NSSortDescriptor和sortedArrayUsingDescriptors

原文:http://www.cnblogs.com/fanyufan/p/3862698.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!