pme*_*ino 5 cocoa nsmutablearray ios
我有两个NSMutableArrays.第一个的内容是数字的,它与第二个的内容配对:
First Array Second Array
45 Test45
3 Test3
1 Test1
10 Test10
20 Test20
Run Code Online (Sandbox Code Playgroud)
这就是两个阵列的外观.现在我怎么能这么数量地命令它们最终如下:
First Array Second Array
1 Test1
3 Test3
10 Test10
20 Test20
45 Test45
Run Code Online (Sandbox Code Playgroud)
谢谢!
Ole*_*ann 17
我会将两个数组放入字典作为键和值.然后,您可以对第一个数组(作为字典中的键)进行排序,并以相同的顺序快速访问字典的值.请注意,这仅在第一个数组中的对象支持时才起作用,NSCopying因为这是NSDictionary有效的.
以下代码应该这样做.它实际上很短,因为NSDictionary提供了一些不错的便利方法.
// Put the two arrays into a dictionary as keys and values
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:secondArray forKeys:firstArray];
// Sort the first array
NSArray *sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
// Sort the second array based on the sorted first array
NSArray *sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
Run Code Online (Sandbox Code Playgroud)