Sorted Array Objects by name

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
JokerMartini's picture

Thanks Swordslayer

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

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Swordslayer's picture

Just use qsort with a custom

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
Nik's picture

!

Thank you Swordslayer

slimatron's picture

best solution for me.... tx

best solution for me.... tx

JokerMartini's picture

Not Updating Variable

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

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

JokerMartini's picture

This is what I came up with.

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

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.