gpt4 book ai didi

iphone - 对 NSMutableArray 进行排序,然后对另一个 NSMutableArray 进行排序

转载 作者:行者123 更新时间:2023-11-29 04:29:28 25 4
gpt4 key购买 nike

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

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

至(从小处开始):

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

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

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

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

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

可以成对订购吗?
提前致谢:)

最佳答案

示例如何对 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);

工作流程:
从数组创建字典 -> 排序字典 -> 从字典创建数组。

编辑

使用 NSSortDescriptorascending: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]];

关于iphone - 对 NSMutableArray 进行排序,然后对另一个 NSMutableArray 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11905724/

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