1

I have an NSArraywith stringsobjects that don't follow a pattern, like:

D C A Z X.

When I try to sort a similar array by ascending order, I get:

A C D X Z.

That's not the result I want, I need to achieve the same pattern as the reference array. I'm just not sure how to do this using NScomparisonResultor NSSortDescriptor.

The second array, may not be exactly the same, but I need to have the same order. for example, maybe I have D X A. Based on the reference array I need to get D A X.

UPDATE:

Ok, so here is my code:

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

     // This is the reference array
     NSArray *labels = [@"Café da Manhã", @"Meio da Manhã", @"Almoço", @"Meio da Tarde", @"Final da Tarde", @"Jantar", @"Pós-Treino"];

    NSSortDescriptor *descriptor = 
    [NSSortDescriptor sortDescriptorWithKey:@"refeicao" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
        NSUInteger index1 = [labels indexOfObject:obj1];
        NSUInteger index2 = [labels indexOfObject:obj2];
        return index1 - index2;
    }];

    [query orderBySortDescriptor:descriptor];
    return query;
}

I have an array of PFObjects, which I need to order by the key refeicao.

6
  • What is your main array contain ? Commented Feb 19, 2014 at 11:44
  • 1
    And everything in your array to be sorted is guaranteed to be in your reference array? Commented Feb 19, 2014 at 11:45
  • Yes. the reference arraywill always be complete. The array to be sorted may be smaller than the reference. Commented Feb 19, 2014 at 11:46
  • Not exactly a duplicate questions, but the answers may apply here as well: stackoverflow.com/questions/16517290/… Commented Feb 19, 2014 at 11:59
  • 1
    @Jorge: Well what result do you get? - I am not familiar with parse.com, but could it be that PFQuery does not support Objective-C/block-based sort descriptors (similar to Core Data) ? In that case you would have to fetch the data first and then sort the fetched array. Commented Feb 19, 2014 at 12:28

2 Answers 2

3

You will need to write your own comparison method (which can tie in with a sort descriptor as you can implement it as an NSComparator).

This method will need to find the 2 strings being compared in your reference array and use the index positions for the comparison. The actual values will not be compared, everything is about the position of the corresponding strings in the reference array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I just tried the code above, but still can't get the result.
You need to return NSComparisonResult, not a subtraction result (you don't know how out of range values might be wrapped). Consider perhaps: return [@(index1) compare:@(index2)]; (and describe how your solution doesn't work - what does it do?)
2

This is kind-of an answer to your original, more general question. It may not be optimally efficient, but looks O(N)-ish. I suspect it's faster than comparators. This assumes that jumbledArray is a subset of referenceArray; you could get around that if needed.

NSArray *referenceArray = @[@"S", @"E", @"T", @"Z", @"R", @"U", @"L"];
NSArray *jumbledArray = @[@"R", @"E", @"S", @"T"];

NSMutableOrderedSet *setToOrder = [[NSMutableOrderedSet alloc] initWithArray:jumbledArray];

NSUInteger insertIndex = 0;

for (NSString *refString in referenceArray) {
    NSUInteger presentIndex = [setToOrder indexOfObject:refString]; // one lookup, presumably cheap

    if (presentIndex != NSNotFound) {
        [setToOrder moveObjectsAtIndexes:[NSIndexSet indexSetWithIndex:presentIndex] toIndex:insertIndex];
        insertIndex++;
    }
}
// [setToOrder array] == @[@"S", @"E", @"T", @"R"]

You could generalize this as a method, something like:

- (NSArray *)arrayBySortingArray:(NSArray *)jumbledArray usingReferenceArray:(NSArray *)referenceArray

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.