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

// 数组用系统方法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]);

    }

posted on 2016-04-09 16:47  NSSong  阅读(1574)  评论(0编辑  收藏  举报