你需要先轉換你的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