You need first convert your categoryNames
array into dictionary
with NSString
key and NSNumber
int
value, the value will be the order in the array
//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++;
}
After that then you can get the value and order with sortedArrayUsingComparator
method, something like this
//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);
}
And voila in sorted
you will have your array of object sorted by your first array NSString
order
Hope this helps