ScriptSpot

Forum topic · General Scripting

Sorted Array Objects by name

By JokerMartini · 2011-06-18

Description

I need to sort the objects in the array by there name without replacing the array with just there names.
The first printed list with all the objects information is what I need, but I need it to be in alphabetical order. The second list is in the correct order but I loose all the objects information.....


arr = selection as array
print arr -- need this array to be sorted by name but still keep all there information
listed = sort(for i in arr collect i.name)
print listed

Comments (7)

Thanks Swordslayer

JokerMartini · 2011-06-19

Thank you for clearing that up and making it a lot simpler than I was doing it.
JokerMartini

Swordslayer · 2011-06-18

Just use qsort with a custom function.

fn compareNames str1 str2 = stricmp str1.name str2.name

MyCustomObjs = for obj in selection where (classOf obj != XRefObject) collect obj
qSort MyCustomObjs compareNames
print MyCustomObjs

slimatron · 2014-11-19

best solution for me.... tx

!

Nik · 2015-05-13

Thank you Swordslayer

deshu · 2019-06-18

Sooo clean. Thanks!

Not Updating Variable

JokerMartini · 2011-06-18

I'm not sure why but this is not updating the outer variable MyCustomObjs?????


MyCustomObjs = #()

fn fnAddItmsLst arr = ( -- add Objects to list
userSel = for obj in selection where (classOf obj != XRefObject) collect obj --xref objects don't have controllers

if userSel.count >= 1 do
(
for o in userSel do (appendIfUnique arr o) -- apend new object to array
listed = sort(for i in arr collect i.name) -- local "listed" array is sorted by name
arr = for o in listed collect getnodebyname o exact:true --arr is built using sorted listed array
)
return arr
print arr
)

fnAddItmsLst MyCustomObjs

print "-----"
print MyCustomObjs

This is what I came up with.

JokerMartini · 2011-06-18

Not sure if its the best solution but this is what I came up with.


arr = selection as array
listed = sort(for i in arr collect i.name)
arr = #()
for i=1 to listed.count do
(
newNode = getnodebyname (listed[i] as string) exact:true
appendIfUnique arr newNode
)
clearListener()
print arr