ios – 基于一个订购两个NSMutableArrays

ios – 基于一个订购两个NSMutableArrays,第1张

概述我有两个NSMutable Arrays.第一个的内容是数字的,它与第二个的内容配对: First Array Second Array 45 Test45 3 Test3 1 Test1 10 Test10 20 Test20 我有两个NSMutable Arrays.第一个的内容是数字的,它与第二个的内容配对:

First Array    Second Array   45              Test45   3               Test3   1               Test1   10              Test10   20              Test20

这就是两个阵列的外观.现在我怎么能这么数量地命令它们最终如下:

First Array    Second Array   1               Test1   3               Test3   10              Test10   20              Test20   45              Test45

谢谢!

解决方法 我会将两个数组放入字典作为键和值.然后,您可以对第一个数组(作为字典中的键)进行排序,并以相同的顺序快速访问字典的值.请注意,这仅在第一个数组中的对象支持NScopying时才有效,因为这是NSDictionary的工作方式.

以下代码应该这样做.它实际上很短,因为NSDictionary提供了一些不错的便利方法.

// Put the two arrays into a dictionary as keys and valuesNSDictionary *dictionary = [NSDictionary dictionaryWithObjects:secondarray forKeys:firstArray];// Sort the first arrayNSArray *sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];// Sort the second array based on the sorted first arrayNSArray *sortedSecondarray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
总结

以上是内存溢出为你收集整理的ios – 基于一个订购两个NSMutableArrays全部内容,希望文章能够帮你解决ios – 基于一个订购两个NSMutableArrays所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1016293.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存