首页 > 编程语言 > 详细

iphone开发数组排序(数组中包括字典)

时间:2016-05-13 05:13:07      阅读:143      评论:0      收藏:0      [点我收藏+]

原文地址

http://blog.csdn.net/bihailantian1988/article/details/8433812


iphone开发数组排序(数组中包括字典)  

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);


本文出自 “ZhuoKing” 博客,请务必保留此出处http://9951038.blog.51cto.com/9941038/1772562

iphone开发数组排序(数组中包括字典)

原文:http://9951038.blog.51cto.com/9941038/1772562

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