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.
arraywill always be complete. The array to be sorted may be smaller than the reference.