Notifications
Clear all

[Closed] sorting selection

hi there…
i’ve been searching in reference and this forum but i didn’t find it.
my question is:
if i select a bunch of object in the scene using rectangle selection tool (click and drag in the viewport) then how to sort those selection in order?( A to Z and Z to A),using maxscript.
if i type:
Print selection

it will return random name order

Need help please…

4 Replies
1 Reply
(@denist)
Joined: 10 months ago

Posts: 0

here is how I do it:


(
 fn sortByName n1 n2 dir:1 = dir*(stricmp n1.name n2.name)
 nodes = selection as array
 
 -- ascending order 
 qsort nodes sortByName dir:1
 
 -- descending order 
 qsort nodes sortByName dir:-1
)

Hi.

I’ve come up with this. Don’t know if it’s the most optimized way to do it, but it’s simple:

fn compareFn el1 el2 sortTypeAsc: =
(
	case of
	(
		(el1.name < el2.name): if sortTypeAsc then -1 else 1
		(el1.name > el2.name): if sortTypeAsc then 1 else -1
		default: 0
	)
)

fn sortSelection selBuffer asc:true =
(
	return (qsort selBuffer compareFn sortTypeAsc:asc)
)

sortedSelection = $selection as Array

for n in sortedSelection do format "%
" n

sortSelection sortedSelection

for n in sortedSelection do format "%
" n

Take a look to the sortSelection function. By default it sorts the array in ascending order by setting the asc parameter equals to true, but you can set descending order setting that parameter to false, like:

sortSelection sortedSelection asc:false

Hope that helps.

hi, Halfvector & DenisT
Thanks for taking time answering my question…
Halfvector’s solution are longer but i can use it…
I don’t know how to use DenisT’s script, i just try select some object then run the script but just return OK.
How to use it DenisT?
Also i want to use the list of selection that has been ordered as an object array so it will be

#($Box:Box01 @[0,0,0], $Box:Box0s @[10,0,0],$Cylinder:Cylinder01 @[30,30,30],…and so on.)

not as string array or object-name string array
Cheers…

Hi.

denisT method is nicer and works perfectly. It returns OK because that’s what qsort function returns when everything is OK. Once you have called qsort function, the nodes array is what contains all your objects sorted as expected.

Here is an example using denisT code:

(
 fn sortByName n1 n2 dir:1 = dir*(stricmp n1.name n2.name)
 
 nodes = selection as array
 
 -- ascending order 
 qsort nodes sortByName dir:1

 -- list nodes in ascending order
 for n in nodes do format "% has class %
" n.name (classof n)
	
 -- descending order 
 qsort nodes sortByName dir:-1

 -- list nodes in descending order
 for n in nodes do format "% has class %
" n.name (classof n)
)

And don’t worry, the “nodes” array is an array which contains the selected objects with all their properties.