gpt4 book ai didi

iphone - 排序 2D NSMutableArray

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:32:32 24 4
gpt4 key购买 nike

我有如下二维数组:

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

谁能帮帮我,提前致谢...

最佳答案

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

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

关于iphone - 排序 2D NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10477575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com