Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How to reverse an array?

How to reverse an array?
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 11, 2002, 02:22 PM
 
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
Reply With Quote
May 11, 2002, 05:38 PM
 
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
Reply With Quote
May 11, 2002, 06:01 PM
 
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
Reply With Quote
May 11, 2002, 06:15 PM
 
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
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 12, 2002, 11:09 AM
 
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
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:58 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2