码迷,mamicode.com
首页 > 编程语言 > 详细

数组排序(按数组中字典的某一键值对)

时间:2015-06-18 16:57:53      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

1。普通数组排序:

     NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"0",@"8",@"6",@"1",nil];
     NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
if ([obj1 intValue] > [obj2 intValue]){
return NSOrderedDescending;
}
if ([obj1 intValue] < [obj2 intValue]){
return NSOrderedAscending;
}
return NSOrderedSame;
}];
      NSLog(@"排序后的数组:%@",sortedArray);
 
2.数组中有字典的排序:
    NSMutableArray *myMutableArr = [[[NSMutableArray alloc] init] autorelease];
    NSDictionary *dicOne = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"price",@"2",@"number", nil];
    NSDictionary *dicTWo = [NSDictionary dictionaryWithObjectsAndKeys:@"6",@"price",@"5",@"number", nil];
    NSDictionary *dicThree = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"price",@"1",@"number", nil];
    NSDictionary *dicFour = [NSDictionary dictionaryWithObjectsAndKeys:@"4",@"price",@"3",@"number", nil];
    [myMutableArr addObject:dicOne];
    [myMutableArr addObject:dicTWo];
    [myMutableArr addObject:dicThree];
    [myMutableArr addObject:dicFour];
    
    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES]];
    [myMutableArr sortUsingDescriptors:sortDescriptors];
    NSLog(@"排序后的数组%@",myMutableArr);

 

数组排序(按数组中字典的某一键值对)

标签:

原文地址:http://www.cnblogs.com/moyazi/p/4586123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!