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

编程入门 行业动态 更新时间:2024-10-09 16:31:57
本文介绍了对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进行排序

本文发布于:2023-10-12 16:21:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485257.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSMutableArray

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!