Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse the data in NSMutableArray? [duplicate]

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?

like image 817
iPhone Avatar asked Nov 26 '11 04:11

iPhone


People also ask

How do you reverse an NSArray in Swift?

In the Swift array, we can reverse the array. To reverse the array we use the reverse() function. This function reverses the order of the specified array. It is the easiest way to reverse the array.

What is NSMutableArray in Swift?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .


2 Answers

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];
like image 86
rob mayoff Avatar answered Sep 30 '22 22:09

rob mayoff


You can try like this way:

for (int k = [originalArray count] - 1; k >= 0; k--) {
    [reverseArray addObject:[originalArray objectAtIndex:k]];
}
like image 39
Leena Avatar answered Sep 30 '22 21:09

Leena