 |
 |
How to reverse an array?
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
Is there an easy way to reverse an array?
I'm using sortedArrayUsingSelector: to sort it initially, but I would like the reversed sort, if you know what I mean. I started going through and reversing the NSOrderedAscending and NSOrderedDescending in the selector method, but that just didn't seem like a very Cocoaish way to do it.
thanks,
kman
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status:
Offline
|
|
haven't actually tried this, but
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
reverseArray = [[someArray reverseObjectEnumerator] allObjects];
</font>[/code]
would be my first guess.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status:
Offline
|
|
Originally posted by kman42:
<STRONG>Is there an easy way to reverse an array?
I'm using sortedArrayUsingSelector: to sort it initially, but I would like the reversed sort, if you know what I mean. I started going through and reversing the NSOrderedAscending and NSOrderedDescending in the selector method, but that just didn't seem like a very Cocoaish way to do it.
thanks,
kman</STRONG>
Depending on your application you might not need to actually reverse the array. A good way to avoid doing to much work is to just store an extra variable containing the sort order, i.e. BOOL sortedAscending; When you want to reverse the array, just flip the variable. Then use the following code to access items in the array:
if (sortedAscending)
return [array objectAtIndex:i];
else
return [array objectAtIndex  [array count] - 1 - i)];
This approach works great if you need to provide data to a NSTableView or NSOutlineView where the user can change sort order as often as he/she likes. Reversing the array each time can be slow, especially if you have many objects.
/Tobias
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: May 2002
Status:
Offline
|
|
Originally posted by tobli:
<STRONG>
if (sortedAscending)
return [array objectAtIndex:i];
else
return [array objectAtIndex  [array count] - 1 - i)];
</STRONG>
Nice.
Edit: Damn smilies.
[ 05-11-2002: Message edited by: serversurfer ]
|
|
Love,
The Surfer
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status:
Offline
|
|
Originally posted by tobli:
<STRONG>
Depending on your application you might not need to actually reverse the array. A good way to avoid doing to much work is to just store an extra variable containing the sort order, i.e. BOOL sortedAscending; When you want to reverse the array, just flip the variable. Then use the following code to access items in the array:
if (sortedAscending)
return [array objectAtIndex:i];
else
return [array objectAtIndex  [array count] - 1 - i)];
This approach works great if you need to provide data to a NSTableView or NSOutlineView where the user can change sort order as often as he/she likes. Reversing the array each time can be slow, especially if you have many objects.
/Tobias</STRONG>
That is awesome. Thanks for the tip. I actually took a bit of a shortcut here since I haven't itroduced live table sorting yet. I simply changed my table lookup to from ...objectAtIndex:rowIndex... to ...objectAtIndex:[array count]-1-rowIndex... It works just as I wanted.
Thanks,
kman
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|