Notifications
Clear all

[Closed] sorting array containing nodes by node name

hi,

i have an array containing objects that i want to sort by the object names... how is this possible?

here's a the code fragment:

  objFilter = "$" + charName + "_*_Morph*"
   morphObjs = (execute objFilter)
  

i tried sorting morphObjs using


 morphObjs = sort (morphObjs as array)
 
3 Replies

ok i’ve found a solution that works… but is there maybe something more elegant?

		    morphArray = (for o in execute ("$" + charName + "_*_Morph*") collect o.name)
 			
 			morphArray = sort morphArray
 			
 			for i=1 to morphArray.count do
 				morphObjs[i] = execute ("$'" + morphArray[i] + "'")

Try using qsort, look up the help. Here’s a function I made that will do what you need:

 			/*------------------------------------------------------------
			# Name		:	rdt_sortHierarchyAlph <function>
			# Info		:	
			# Parms		:	inputA <object>
			#			inputB <object>
			# Returns	:	<integer>
			--------------------------------------------------------------*/
			function rdt_sortHierarchyAlph inputA inputB =
				(
				if (classof inputB) != array and (classof inputA) == array then return 1
				if (classof inputB) == array and (classof inputA) != array then return -1
				if (classof inputB) == array and (classof inputA) == array then return (stricmp inputA[1].name inputB[1].name)
				if (not (isProperty inputA #name)) or (not (isProperty inputA #name)) then return -1
				if (not (isProperty inputB #name)) or (not (isProperty inputB #name)) then return 1	
				
				return (stricmp inputA.name inputB.name)
				); 

The neat thing about this function is it will sort a multi dimensional array. To use try the following (This assumes that you’ve executed the function I pasted above.):

qsort (objects as array) rdt_sortHierarchyAlph

Thanks a lot Rod! that is, in fact, a lot more elegant