对NSMutableArray进行排序,然后对另一个NSMutableArray进行排序 [英] Sort NSMutableArray and sort another NSMutableArray along

查看:63
本文介绍了对NSMutableArray进行排序,然后对另一个NSMutableArray进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以像这样对数组进行排序:

It's possible to sort an array like this:

MutableArray  = [NSMutableArray arrayWithObjects:@"5",@"1",@"7,@"9",@"4", nil];

到(从大处开始,从小处开始):

to (start big ends small):

MutableArray  = [NSMutableArray arrayWithObjects:@"9",@"7",@"5,@"4",@"1", nil];

是否还可以对两个数组进行排序:

Is it also possible to sort two arrays:

MutableArray  = [NSMutableArray arrayWithObjects:@"5",@"1",@"7",@"9",@"4", nil];
MutableArray  = [NSMutableArray arrayWithObjects:@"Andy",@"Mike",@"Bob",@"Amy",@"Alex", nil];

to(从大到小,但是 Andy 得到 5 点, Mike 得到 1 等)):

to (from big to small, but Andy got 5 point, Mike got 1 and so on):

MutableArray  = [NSMutableArray arrayWithObjects:@"9",@"7",@"5",@"4",@"1", nil];
MutableArray  = [NSMutableArray arrayWithObjects:@"Amy",@"Bob",@"Andy",@"Alex",@"Mike", nil];

是否可以将它们作为一对订购?
在此先感谢:)

Is it possible to order them as a couple?
Thanks in Advance :)

推荐答案

示例如何对 arrayOne 升序排序和对 arrayTwo 进行排序:

Example how to sort arrayOne ascending and sort arrayTwo along:

NSMutableArray *arrayOne  = [NSMutableArray arrayWithObjects:@"5",@"1",@"7",@"9",@"4", nil];
NSMutableArray *arrayTwo = [NSMutableArray arrayWithObjects:@"Andy",@"Mike",@"Bob",@"Amy",@"Alex", nil];

NSDictionary *temp = [NSDictionary dictionaryWithObjects:arrayTwo forKeys:arrayOne];
NSArray *sortedArrayOne = [[temp allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSArray *sortedArrayTwo = [temp objectsForKeys:sortedArrayOne notFoundMarker:[NSNull null]];

NSLog(@"%@",sortedArrayOne);
NSLog(@"%@",sortedArrayTwo);

工作流程:
从数组-> 创建字典,对字典-> 进行排序,从字典中创建数组.

Workflow:
Create dictionary from the arrays -> sort dictionary -> create arrays from dictionary.

编辑

使用 NSSortDescriptor ascending:NO 进行降序排序,快速示例:

Use NSSortDescriptor with ascending:NO to sort descending, quick example:

NSDictionary *temp = [NSDictionary dictionaryWithObjects:arrayTwo forKeys:arrayOne];
NSSortDescriptor *theDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(compare:)];    
NSArray *sortedArrayOne = [[temp allKeys] sortedArrayUsingDescriptors:[NSArray arrayWithObject:theDescriptor]];
NSArray *sortedArrayTwo = [temp objectsForKeys:sortedArrayOne notFoundMarker:[NSNull null]];

这篇关于对NSMutableArray进行排序,然后对另一个NSMutableArray进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆