Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何对NSMutableArray进行排序?_Objective C_Sorting - Fatal编程技术网

Objective c 如何对NSMutableArray进行排序?

Objective c 如何对NSMutableArray进行排序?,objective-c,sorting,Objective C,Sorting,我有以下NSMutableArray: NSMutableArray *array1 = [[NSMutableArray alloc]initWithObjects:@"AAA",@"BBB",@"CCC",nil]; NSMutableArray *array2 = [[NSMutableArray alloc]initWithObjects:@"BBB",@"AAA",@"CCC",nil]; NSMutableArray *array3 = [[NSMutableArray alloc]

我有以下NSMutableArray:

NSMutableArray *array1 = [[NSMutableArray alloc]initWithObjects:@"AAA",@"BBB",@"CCC",nil];
NSMutableArray *array2 = [[NSMutableArray alloc]initWithObjects:@"BBB",@"AAA",@"CCC",nil];
NSMutableArray *array3 = [[NSMutableArray alloc]initWithObjects:@"CCC",@"BBB",@"AAA",nil];

NSMutableArray *number = [[NSMutableArray alloc]initWithCapacity:3]; <- use sorting key.

NSNumber *number1 = [[NSNumber alloc]initWithInt:30];
NSNumber *number2 = [[NSNumber alloc]initWithInt:20];
NSNumber *number3 = [[NSNumber alloc]initWithInt:10];
[number addObject:number1];
[number addObject:number2];
[number addObject:number3];

我该怎么做呢?

我想您要求的是将多个数组按与控制数组相同的相对顺序进行排序?
在这种情况下,如果可以对一个数组进行排序,只需对其他数组应用与对第一个数组排序相同的变换即可。

请参阅
-sortedarray使用…
方法的文档。您可能需要实现自己的比较器块。

我将以不同的方式来实现它。我将定义一个包含三个字符串和一个整数的类,并将该类的实例放在单个数组中。对数组进行排序时,将按整数排序,其余的数组将自动随整数一起移动

@interface QuadrupleItems : NSObject
{
    NSString *first;
    NSString *second;
    NSString *third;
    NSInteger integer;
}
// ... methods you might need, e.g. initWithFirst:second:third:integer: etc...
@property (retain) NSString *first;
// etc...
现在,排序很简单,我想您已经知道可以使用
sortedarray:
对数组进行排序。在块中,比较项目的整数属性

@interface QuadrupleItems : NSObject
{
    NSString *first;
    NSString *second;
    NSString *third;
    NSInteger integer;
}
// ... methods you might need, e.g. initWithFirst:second:third:integer: etc...
@property (retain) NSString *first;
// etc...