你需要先转换你的categoryNames
数组dictionary
与NSString
键和NSNumber
int
值,该值将在数组中
//this is example code, this will be your first array (reference value array)
NSArray * array = @[@"prueba",@"prueba2",@"prueba3"];
//first you need convert this array in NSDictionary
NSMutableDictionary * arrayDict = [NSMutableDictionary dictionary];
int counter = 0;
for (NSString * value in array) {
if(arrayDict[value] == nil)
{
arrayDict[value] = [NSNumber numberWithInt:counter];
}
counter++;
}
在此之后,为了那么你就可以得到价值并用sortedArrayUsingComparator
方法订购,类似这样的
//this is an example of your second array categoryTempElements
NSArray * arrayOfObjs = @[[testObject testObjectWithName:@"prueba3"],[testObject testObjectWithName:@"prueba"],[testObject testObjectWithName:@"prueba2"]];
NSArray * sorted = [arrayOfObjs sortedArrayUsingComparator:^NSComparisonResult(testObject * _Nonnull obj1, testObject * _Nonnull obj2) {
if([((NSNumber*)arrayDict[obj1.cName]) intValue] < [((NSNumber*)arrayDict[obj2.cName]) intValue]){
return NSOrderedAscending;
}
if([((NSNumber*)arrayDict[obj1.cName]) intValue] > [((NSNumber*)arrayDict[obj2.cName]) intValue]){
return NSOrderedDescending;
}
return NSOrderedSame;
}];
for (testObject * obj in sorted) {
NSLog(@"%@",obj.cName);
}
瞧在sorted
你将拥有你的第一阵列NSString
为了
希望这有助于
我需要解决方案在目标c – hotspring