5

Possible Duplicate:
How to display an array in reverse order in objective C

I have an NSMutableArray and this array contains information nicely in UITableView. but I want to display latest information first in UITableView. Right now the earliest information comes first in UITableView. My code is as follows:

NSMutableArray *entries = [NSMutableArray array];
[self parseFeed:doc.rootElement entries:entries];
for (RSSEntry *entry in entries) {
    [allEntries insertObject:entry atIndex:0];   //insertIdx];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
}

then How can I reverse the information in NSMutableArray?

Community
  • 1
  • 1
iPhone
  • 4,092
  • 3
  • 34
  • 58

2 Answers2

13

How about just enumerating the contents of entries in reverse order?

for (RSSEntry *entry in [entries reverseObjectEnumerator]) {
    ...
}

If you just want to take an array and create a reversed array, you can do this:

NSArray *reversedEntries = [[entries reverseObjectEnumerator] allObjects];
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    Of course if you really need an array reversed from the original, just fill a second mutable array while reverse enumerating. – NJones Nov 26 '11 at 05:03
7

You can try like this way:

for (int k = [originalArray count] - 1; k >= 0; k--) {
    [reverseArray addObject:[originalArray objectAtIndex:k]];
}
Hamed Rajabi Varamini
  • 3,439
  • 3
  • 24
  • 38
Leena
  • 2,678
  • 1
  • 30
  • 43