[英]Sort NSMutableArray and sort another NSMutableArray along
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 (from big to small, but Andy
got 5
point, Mike
got 1
and so on): 到(从大到小,但
Andy
得到5
分, Mike
得到1
,依此类推):
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 :) 提前致谢 :)
Example how to sort arrayOne
ascending and sort arrayTwo
along: 示例如何对
arrayOne
升序排序和对arrayTwo
进行排序:
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. 从数组创建字典
->
排序字典->
从字典创建数组。
EDIT 编辑
Use NSSortDescriptor
with ascending:NO
to sort descending, quick example: 使用
NSSortDescriptor
和ascending:NO
来排序降序,快速示例:
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]];
Why don't you just create some new object let's say Player
with ivars name
and score
and store instances of that class in array. 为什么不创建一些新对象,比如说具有ivars
name
和score
Player
,并将该类的实例存储在数组中。 Then you'll be able to sort that array as you want for any point in your code like so: 然后,您可以根据需要对代码中的任何点进行排序,如下所示:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] ;
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedByName = [players sortedArrayUsingDescriptors:sortDescriptors];
or by score: 或按分数:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"score" ascending:NO] ;
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedByName = [players sortedArrayUsingDescriptors:sortDescriptors];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.