Alphanumeric sorting in maxscript

Alphanumeric sorting in maxscript

Anonymous
Not applicable
1,245 Views
4 Replies
Message 1 of 5

Alphanumeric sorting in maxscript

Anonymous
Not applicable

Currently using this function by Swordslayer to sort my collection of children in a parent:

fn CompareNames str1 str2 = stricmp str1.name str2.name

Problem is, it doesn't handle alphanumeric names. A lot of the children have a numeric prefix. When treated as a string, like in the function above, 10-name is sorted before 4-name. Trying to get my sorted children to match the order in Max (sorted ascending).

 

Any suggestions on how to sort alphanumerically?

 

Thank you in advance for your help.

0 Likes
Accepted solutions (1)
1,246 Views
4 Replies
Replies (4)
Message 2 of 5

Swordslayer
Advisor
Advisor

Too lazy to write the function myself:

 

fn getSHLWAPI = 
(
	source =  "using System;\n"
	source += "using System.Runtime.InteropServices;\n"
	source += "class SHLWAPI\n"
	source += "{\n"
	source += "	[DllImport(\"shlwapi.dll\", CharSet = CharSet.Auto)]\n"
	source += "	public static extern int StrCmpLogicalW(string lpszStr, string lpszComp);\n"
	source += "}\n"

	csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
				
	compilerParams.GenerateInMemory = on
	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
	compilerResults.CompiledAssembly.CreateInstance "SHLWAPI"
)
if ::SHLWAPI == undefined do SHLWAPI = getSHLWAPI()

x = #("name10", "name4", "name20", "name100")
qsort x SHLWAPI.StrCmpLogicalW --> #("name4", "name10", "name20", "name100")

 

0 Likes
Message 3 of 5

Anonymous
Not applicable

Thank you so much for the solution Swordslayer.

 

Currently using some more of your suggested code to get all the children of a group/parent:

childObjects = for obj in stepParent.children where (classOf obj != XRefObject) collect obj

Problem is, I cannot get the alphanumeric sorting function to work on it. I also tried:

childObjects = parent.children

How can I put the children into an array/collection that qSort will work on?

 

I want to sort the objects and not just the names so that I can manipulate the objects once sorted.

 

Thank you again for your help.

0 Likes
Message 4 of 5

Swordslayer
Advisor
Advisor
Accepted solution

It works on any array, not on collections. Use SHLWAPI.StrCmpLogicalW in place of stricmp in the compareNames function, i.e.

 

fn compareNames str1 str2 = SHLWAPI.StrCmpLogicalW str1.name str2.name
0 Likes
Message 5 of 5

Anonymous
Not applicable

Thank you again. That did it for me.

0 Likes