iphone - 排序 2D NSMutableArray

标签 iphone objective-c ios sorting nsmutablearray

我有如下二维数组:

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/

相关文章:

iphone - 检测用户的旋转运动

objective-c - 我如何使用 UIPageControl?

iphone - 使用 css 多列在 UIWebView 中对 html 进行分页时出错

iphone - SVProgressHUD - 禁用用户交互

objective-c - 使用 [NSString stringWithString : @"some string"] versus @"some string" 的优缺点

iphone - 如何在 iphone 的 ScrollView 中将中间按钮比其他按钮高 5px

iphone - 是否可以用动画移除FromSuperview?

iphone - 使用 AVPlayer 进行播放?

ios - 如果在不支持facebook的国家使用SLServiceTypeFacebook,SLComposeViewController会导致崩溃吗?

ios - UICollectionViewCell 延迟重绘自定义 UIView 子层