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