NSArray中的数据进行排序

NSArray中的数据排序,经常是利用NSSortDescriptor。

官方文档:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html


//组装数据
    array=[[NSMutableArray alloc] initWithCapacity:11];
   
    NSMutableDictionary *dir6=[[NSMutableDictionary alloc] initWithCapacity:1];
    [dir6 setObject:@"曹磊" forKey:@"FriendName"];
    [dir6 setObject:@"曹磊" forKey:@"FriendID"];
    [array addObject:dir6];
    [dir6 release];

   
    NSMutableDictionary *dir=[[NSMutableDictionary alloc] initWithCapacity:1];
    [dir setObject:@"王军" forKey:@"FriendName"];
    [dir setObject:@"曹磊" forKey:@"FriendID"];
    [array addObject:dir];
    [dir release];
   
    NSMutableDictionary *dir1=[[NSMutableDictionary alloc] initWithCapacity:1];
    [dir1 setObject:@"111" forKey:@"FriendName"];
    [dir1 setObject:@"曹磊" forKey:@"FriendID"];
    [array addObject:dir1];
    [dir1 release];
   
    NSMutableDictionary *dir2=[[NSMutableDictionary alloc] initWithCapacity:1];
    [dir2 setObject:@"王兴朝" forKey:@"FriendName"];
    [dir2 setObject:@"曹磊" forKey:@"FriendID"];
    [array addObject:dir2];
    [dir2 release];
   
    NSMutableDictionary *dir3=[[NSMutableDictionary alloc] initWithCapacity:1];
    [dir3 setObject:@"wangjun" forKey:@"FriendName"];
    [dir3 setObject:@"曹磊" forKey:@"FriendID"];
    [array addObject:dir3];
    [dir3 release];
   
    NSMutableDictionary *dir4=[[NSMutableDictionary alloc] initWithCapacity:1];
    [dir4 setObject:@"apk" forKey:@"FriendName"];
    [dir4 setObject:@"曹磊" forKey:@"FriendID"];
    [array addObject:dir4];
    [dir4 release];
   
   
    self.myArray=array;
    [array release];
    NSLog(@">>>>>>排序前>>>>>>>>");
    for (int i=0; i<[self.myArray count]; i++) {
        NSLog(@"%@",[[self.myArray objectAtIndex:i] objectForKey:@"FriendName"]);
    }
   
    NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"FriendName" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sorter count:1];
    NSArray *sortedArray = [self.myArray sortedArrayUsingDescriptors:sortDescriptors];

   
    [sorter release];

    NSLog(@"\n\n\n\n\n>>>>>>排序后>>>>>>>>");
    for (int i=0; i<[sortedArray count]; i++) {
        NSLog(@"%@",[[sortedArray objectAtIndex:i] objectForKey:@"FriendName"]);
    }

 

数据结果:

2011-04-21 18:31:36.556 sort[4041:707] >>>>>>排序前>>>>>>>>
2011-04-21 18:31:36.567 sort[4041:707] 曹磊
2011-04-21 18:31:36.571 sort[4041:707] 王军
2011-04-21 18:31:36.575 sort[4041:707] 111
2011-04-21 18:31:36.578 sort[4041:707] 王兴朝
2011-04-21 18:31:36.582 sort[4041:707] wangjun
2011-04-21 18:31:36.585 sort[4041:707] apk
2011-04-21 18:31:36.589 sort[4041:707]

 

>>>>>>排序后>>>>>>>>
2011-04-21 18:31:36.595 sort[4041:707] 111
2011-04-21 18:31:36.599 sort[4041:707] apk
2011-04-21 18:31:36.602 sort[4041:707] wangjun
2011-04-21 18:31:36.606 sort[4041:707] 曹磊
2011-04-21 18:31:36.610 sort[4041:707] 王兴朝
2011-04-21 18:31:36.613 sort[4041:707] 王军


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值