Alex Rivera | Logout

How do I reverse the order of objects in an NSMutableArray?

Asked 2010-01-16T23:31:11.187
37

I'm trying to achieve something like this

NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"A",@"B",@"C", nil];
NSLog(@"Array: %@", myArray);
//logs A, B, C.

//reverse code here

 NSLog(@"Array: %@", myArray);
//logs C, B, A

The code isn't exact, just a demo. But you get the idea.

Any way to do this?

Edit
Report

1 Answer

164

Simply use;

NSArray* reversed = [[myArray reverseObjectEnumerator] allObjects];

The order is documented to be correct:

This array contains all the remaining objects of the enumerator in enumerated order [emphasis added].

answered 2010-01-16T23:42:00.883

Your Answer