奥非域

ios二维数组排序

    //初始化二维数组
    NSArray *sortArray =
	 [[NSArrayalloc] initWithObjects:[NSArrayarrayWithObjects:@"file1",@"adsfsa", nil],
          [NSArrayarrayWithObjects:@"file4",@"asfdasfs", nil],
          [NSArrayarrayWithObjects:@"file2",@"wefxdfsf", nil],
          [NSArrayarrayWithObjects:@"file3",@"fgsdaq", nil],
          nil];
    //将二维数组赋值给字典
    NSMutableDictionary *dic=[[NSMutableDictionaryalloc] init];
    for (int i=0; i<sortArray.count; i++) {
        [dic setObject:sortArray[i][1] forKey:sortArray[i][0]];
    }

    //把key进行降序排序
    NSArray* arr1 = [dic allKeys];
    arr1 = [arr1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
        NSComparisonResult result = [obj1 compare:obj2];
        return result==NSOrderedDescending;
    }];
    //按照降序输出
    for(NSString* str in arr1)
    {
        NSLog(@"%@:%@",str, [dic objectForKey:str]);
    }
    
    NSLog(@"==============");
    
    //把key进行升序排序
    NSArray* arr = [dic allKeys];
    arr = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
        NSComparisonResult result = [obj1 compare:obj2];
        return result==NSOrderedAscending;
    }];
    //按照升序输出
    for(NSString* str in arr)
    {
        NSLog(@"%@:%@",str, [dic objectForKey:str]);
    }

结果:

2016-04-13 21:27:47.538 Sorting contrast[3553:261277] file1:adsfsa
2016-04-13 21:27:47.539 Sorting contrast[3553:261277] file2:wefxdfsf
2016-04-13 21:27:47.539 Sorting contrast[3553:261277] file3:fgsdaq
2016-04-13 21:27:47.539 Sorting contrast[3553:261277] file4:asfdasfs
2016-04-13 21:27:47.539 Sorting contrast[3553:261277] ==============
2016-04-13 21:27:47.539 Sorting contrast[3553:261277] file4:asfdasfs
2016-04-13 21:27:47.540 Sorting contrast[3553:261277] file3:fgsdaq
2016-04-13 21:27:47.540 Sorting contrast[3553:261277] file2:wefxdfsf
2016-04-13 21:27:47.540 Sorting contrast[3553:261277] file1:adsfsa

IOS