2010-11-23 29 views
2

Possible en double:
How can I reverse a NSArray in Objective-C?Est-il possible de convertir NSMutableArray dans son ordre inverse dans l'iPhone?

J'ai NSMutableArray *mutArray = [[NSMutableArray alloc]initWithObjects:obj1,obj2,obj3,nil]; et chaque objet ont nom, titre, adresse mais je veux montrer mutArray dans un autre NSMutableArray qui montre dans l'ordre inverse, il signifie obj3, obj2 & puis obj1, nil comment je le fais.

+0

déjà répondu ici http://stackoverflow.com/questions/586370/how-can-i-reverse-a-nsarray-in-objective-c – Rog

Répondre

6

Vous pouvez le faire sur cette voie:

NSMutableArray* reversedArray = [[NSMutableArray alloc] initWithArray:[[[[NSArray alloc] initWithArray: mutArray] reverseObjectEnumerator] allObjects]]; 
0

j'ai fait ce qui suit et cela a fonctionné pour moi:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] 
       initWithKey:@"key" ascending:YES]; 

[mutableArray sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]]; 

mutableArray = [mutableArray copy];

[descriptor release]; 

Hope it helps!