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:@"device_uuid"];

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

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

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

    [dict setObject:@"5" forKey:@"interface_type"];

    [dict setObject:@"6" forKey:@"timestamp"];

    [dict setObject:@"7" forKey:@"key"];

 

    NSArray *keys = [dict allKeys];

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

        return [obj1 compare:obj2 options:NSNumericSearch];

    }];

    

    NSLog(@"sortedArray----%@",sortedArray);

    

    NSMutableArray * valueArray = [NSMutableArray array];

    

    for (NSString *keyString in sortedArray) {

        

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

        NSString * valueString = [dict objectForKey:keyString];

        [valueArray addObject:valueString];

        

    }

 

    NSString *tempString = [valueArray componentsJoinedByString:@","];

    

 

    NSLog(@"tempString---%@",tempString);

    

    

 

posted @ 2019-09-06 10:16  iOS张文权  阅读(828)  评论(0编辑  收藏  举报