PDA

View Full Version : help with array orders


gonemad
10-17-2005, 06:37 PM
Hi all, wondering if anyone could lend a hand?

I need to sort the items within an array based upon there name...ex: box01, box02, box03

Right now I have 300 or so items, they were not created in sequence but they were selected in sequence and renamed so that their names would follow the order they were selected. Now when I either do (selection as array) or $box* as array for instance it still lists them within the array based on the order they were created and not the order in which they're named or selected. I've been looking around for reference on how to rearrange this data into the order I need it but I'm comming up dry.

Any help would be much appreciated. Thanks.
-mad

eek
10-17-2005, 06:53 PM
sort <array>

Sorts the elements of the array into ascending order. All the elements must be comparable.


eek

gonemad
10-17-2005, 07:31 PM
Thanks for the quick reply eek,

I've tried using sort but it dosn't sort the items by their name, it still lists them in the order in which they were created.

Say I have 5 items I'm trying to sort and they're all named sequentially temp_1-5 my code looks something like this


myarray = ($temp_* as array)
print myarray


Now they were named in order from 1 to 5, but they were created in the order of 1,3,5,2,4
so when I print the array it gives me:

$Box:temp_1 @...
$Box:temp_3 @...
$Box:temp_5 @...
$Box:temp_2 @...
$Box:temp_4 @...
OK

when I append the code so that it reads

myarray = ($temp_* as array)
sort myarray
print myarray


it prints the same result as before.

I'm a complete novice at this, this is only my second day playing around with maxscript at all, so I'm sure there is either something I'm not doing right or some simple solution that I don't yet know about. Thanks again for your help.

Bobo
10-17-2005, 08:06 PM
I'm a complete novice at this, this is only my second day playing around with maxscript at all, so I'm sure there is either something I'm not doing right or some simple solution that I don't yet know about. Thanks again for your help.


Sort will only work on values that can be compared (numbers, strings).

One quick way to sort an array of nodes (as long as all of them have UNIQUE names!) would be

theNames = for o in $temp_* collect o.name
sort theNames
theSortedArray = for n in theNames collect getNodeByName n


or in a single line,

theSortedArray = for n in ( sort (for o in $temp_* collect o.name)) collect getNodeByName n



If there are nodes with identical names, you would have to use the QSORT method with indexed sort. Since this method is more advanced, I will not go into details - you can check out the MXS Reference if you want.

Rivendale
10-17-2005, 08:11 PM
Hey, here is an alternative version using qsort like Bobo suggested:

allobjs = $ as array --array of selected objects
fn Namesort obj1 obj2 = stricmp obj1.name obj2.name --function to sort names
qsort allobjs Namesort --sort array using above function
print allobjs --print result


cheers,
CML

gonemad
10-17-2005, 08:21 PM
Thanks a bunch Bobo, you da' man. I'll give this a try when I get home from work.

CGTalk Moderation
10-17-2005, 08:21 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.