Iphone 排序2D NSMutableArray

Iphone 排序2D NSMutableArray,iphone,objective-c,ios,sorting,nsmutablearray,Iphone,Objective C,Ios,Sorting,Nsmutablearray,我有如下2D数组: 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]; sc

我有如下2D数组:

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
谁能帮帮我吗, 提前感谢…

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

NSArray *sortedWorld = [world sortedArrayUsingComparator:^(id a, id b) {
    NSNumber *numA = [a objectAtIndex:1];
    NSNumber *numB = [b objectAtIndex:1];
    return [numB compare:numA];
}];

我正在使用这个NSSortDescriptor*sortOrder=[NSSortDescriptor sortdescriptor with key:@“number”升序:NO];[世界排序描述符:[NSArray ARRAY WITHOBJECT:sortOrder]];您是否尝试使用NSSortdescriptor?我已经尝试过了,但它给了我以下错误::“NSUnknownKeyException”,原因:“[valueForUndefinedKey:]:该类不符合键值编码的键值编码要求。好的,那么您希望对数字或单词进行哪一行排序?我想以降序方式对数组进行排序。非常感谢,亲爱的。