码迷,mamicode.com
首页 > 移动开发 > 详细

iOS简单排序--字母排序、NSDictionary排序

时间:2016-04-09 18:36:54      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

// 数组用系统方法compare做字母的简单排序

    NSArray *oldArray = @[@"bac",@"bzd",@"azc",@"azz"];

  NSArray *newArray = [oldArray sortedArrayUsingSelector:@selector(compare:)];

    NSLog(@"new array = %@",newArray);

    

// 因为NSDictionary排序

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

    [dict setObject:@"1" forKey:@"key1"];

    [dict setObject:@"2" forKey:@"key2"];

    [dict setObject:@"3" forKey:@"key3"];

    [dict setObject:@"4" forKey:@"key4"];

    

   for (NSString *str in [dict allKeys]) {

       NSLog(@"key == %@",str);

    }

    

   NSArray *keys = [dict allKeys];

   NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){

        return [obj1 compare:obj2 options:NSNumericSearch];

    }];

    

   for (NSString *categoryId in sortedArray) {

        NSLog(@"[dict objectForKey:categoryId] === %@",[dict objectForKey:categoryId]);

    }

iOS简单排序--字母排序、NSDictionary排序

标签:

原文地址:http://www.cnblogs.com/NSong/p/5372002.html

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