在Objective-C中对NSArray和NSMutableArray进行排序

用户名

我有一个NSArray包含6个带有图像像素数据的3D数组,以及一个具有6个值的NSMutableArray。我想对NSMutableArray数组进行数字排序,并按照与NSMutableArray排序相同的顺序对NSArray进行排序。我知道如何在python中执行此操作,但是我对Objective-C不太满意

即:从:NSArray = [img1,img2,img3] NSMutableArray = [5,1,9]到:NSArray = [img2,img1,img3] NSMutableArray = [1,5,9]

NSArray *imageArray = [...some images];

NSMutableArray *valueList = [[NSMutableArray alloc] initWithCapacity:0];

float value1 = 5.0;
float value2 = 1.0;
float value3 = 9.0;

[valueList addObject:[NSDecimalNumber numberWithFloat:value1]];

[valueList addObject:[NSDecimalNumber numberWithFloat:value2]];

[valueList addObject:[NSDecimalNumber numberWithFloat:value3]];
阿敏·内格姆·阿瓦德

从一开始就可能使用错误的数据结构。您不应该提出不同数组中的键值对。然而, …

首先创建一个字典将两个列表配对,然后对键进行排序,最后检索值:

NSArray *numbers = …; // for sorting
NSArray *images = …;  // content

// Build pairs
NSDictionary *pairs = [NSDictionary dictionaryWithObjects:images forKeys:numbers];

// Sort the index array
numbers = [numbers sortedArrayByWhatever]; // select a sorting method that's comfortable for you

// Run through the sorted array and get the corresponding value
NSMutableArray *sortedImages = [NSMutableArray new];
for( id key in numbers )
{
   [sortedImages appendObject:pairs[key]];
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章