2

我有二维数组,如下所示:

NSMutableArray *world=[[NSMutableArray alloc]init];
NSNumber *number;
NSMutableArray *inner;

int scr=2;
number=[[NSNumber alloc]initWithInt:scr];
inner=[[NSMutableArray alloc]initWithObjects:@"ABC",number,nil];
[world addObject:inner];

scr=6;
number=[[NSNumber alloc]initWithInt:scr];
inner=[[NSMutableArray alloc]initWithObjects:@"XYZ",number,nil];
[world addObject:inner];

scr=1;
number=[[NSNumber alloc]initWithInt:scr];
inner=[[NSMutableArray alloc]initWithObjects:@"PQR",number,nil];
[world addObject:inner];

scr=5;
number=[[NSNumber alloc]initWithInt:scr];
inner=[[NSMutableArray alloc]initWithObjects:@"DEF",number,nil];
[world addObject:inner];

scr=3;
number=[[NSNumber alloc]initWithInt:scr];
inner=[[NSMutableArray alloc]initWithObjects:@"LMN",number,nil];
[world addObject:inner];

现在,我想对世界数组进行排序以获得以下结果,

XYZ 6
DEF 5
LMN 3
ABC 2
PQR 1

谁能帮帮我,在此先感谢...

4

1 回答 1

6

您可以使用sortedArrayUsingComparator:自定义排序代码块对数组进行排序。试试这个:

NSArray *sortedWorld = [world sortedArrayUsingComparator:^(id a, id b) {
    NSNumber *numA = [a objectAtIndex:1];
    NSNumber *numB = [b objectAtIndex:1];
    return [numB compare:numA];
}];
于 2012-05-07T06:33:15.333 回答