Help sorting an array please!

Help sorting an array please!

Anonymous
Not applicable
457 Views
3 Replies
Message 1 of 4

Help sorting an array please!

Anonymous
Not applicable
Hi guys,

Can anybody tell me a simple method of sorting an array of scene objects by poly count? I have tried and failed to use qsort to do this. I think the problem lies between chair and keyboard.

Thanks in advance,
Nik
0 Likes
458 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
Not sure why your qsort didn't work, but here is an example for you. This will sort the current selection by polycount. If you want to sort all geometry instead of the current selection replace "selection as array" with "geometry as array".


fn Sort_By_Polycount elementOne elementTwo =
(
case of
(
((getPolygonCount elementOne) < (getPolygonCount elementTwo)): -1
((getPolygonCount elementOne) > (getPolygonCount elementTwo)): 1
default: 0
)--end case
)--end fn

mySelection = selection as array
for current in mySelection do
(
print (current.name + " - " + ((getPolygonCount current) as string))
)--end for

qsort mySelection Sort_By_Polycount

for current in mySelection do
(
print (current.name + " - " + ((getPolygonCount current) as string))
)--end for

/******************RESULTS*********************
Sort_By_Polycount()
#($Sphere:Sphere02 @ , $Sphere:Sphere03 @ ,
$Sphere:Sphere01 @ )
"Sphere02 - 224"
"Sphere03 - 960"
"Sphere01 - 48"
OK
OK
"Sphere01 - 48"
"Sphere02 - 224"
"Sphere03 - 960"
OK
**********************************************/
0 Likes
Message 3 of 4

Anonymous
Not applicable
Awesome. I can't thank you enough! This works perfectly.

I immediately see what I was doing wrong. I wasn't bracketing correctly around the getPolygonCount elementOne part of the code.

Thank you again, you have made my morning (and taught me something!)

Regards,
Nik
0 Likes
Message 4 of 4

Anonymous
Not applicable



fn Sort_By_Polycount elementOne elementTwo =
(
case of
(
((getPolygonCount elementOne) < (getPolygonCount elementTwo)): -1
((getPolygonCount elementOne) > (getPolygonCount elementTwo)): 1
default: 0
)--end case
)--end fn

it does the same but much faster:

fn Sort_By_Polycount elementOne elementTwo = (getPolygonCount elementOne) - (getPolygonCount elementTwo)
0 Likes