0

I have the following categoryNames array.

enter image description here

And now, I have categoryTempElements and it has cName property. I need to know how to order categoryTempElements with a order of categoryNames.

enter image description here

UPDATE: I have added sortOrder property to Category object and tried the following but order does not change.

for (Category* a in categoryTempElements) {
      int index = (int)[categoryNames indexOfObject:a.cName];
      a.sortOrder = index;
}

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [categoryTempElements sortedArrayUsingDescriptors:sortDescriptors];
casillas
  • 16,351
  • 19
  • 115
  • 215

4 Answers4

1

Another way could be without using sortedArrayUsingComparator, using two for-loops.
Declare a new Mutable array called sortedCategoryElements and compare the categoryNames in categoryTempElements, If matches add it to a new array sortedCategoryElements:

NSMutableArray *sortedCategoryElements = [NSMutableArray new];
for (NSString *name in categoryNames) {
    for (Category *category in categoryTempElements) {
        if (name == category.cName) {
            [sortedCategoryElements addObject:category];
            break;
        }
    }
}

I tried with your set of data, it worked for me.
Hope it helps!

Imad Ali
  • 3,261
  • 1
  • 25
  • 33
0

Create a dictionary with cName as the key and the Category object as the value. Then iterate over the categoryNames array, and build another array by using each item in categoryNames as the key. The resulting array should be sorted in the same order as categoryNames.

rounak
  • 9,217
  • 3
  • 42
  • 59
0

NSArray has a method sortedArrayUsingComparator: which sorts an array using the ordering implemented by the block you pass. This block, of type NSComparator, is passed references to two elements of your array and you must return the order of those two elements.

And now, I have categoryTempElements and it has cName property.

So your block will be passed two categoryTempElements, you need to access the cName property of each, and compare the resulting two values...

I need to know how to order categoryTempElements with a order of categoryNames

by the position, i.e. the index, of those values in your categoryNames array. The method indexOfObject: provides that index for you.

So put that together and your problem is solved.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • I have updated my question with the approach that you have suggested but I am getting the same order, ordering did not change. – casillas Aug 20 '17 at 03:48
  • This answer does not suggest you add any properties, or use sort descriptors - neither are required - and that is what your current edit shows. Read the documentation for the methods and types I mentioned, read the steps in the answer, and you'll have your solution in a few lines of code. If you get stuck you can edit your question, or ask a new one, showing what you've done and explaining what doesn't work. HTH – CRD Aug 20 '17 at 05:32
0

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

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55