Sorting by second word

hey all,
i’m trying to sort a group of files in a folder alphabetically by the second word of the filename. I’ve tried a bunch of stuff but I can’t come up with anything that comes remotely close to working. I was hoping someone might have an idea or simply tell me it’s a fools quest so i can give up.
thanks.

Try this one:

set x to {alias "path:to:some stuff", alias "path:to:some things", alias "path:to:some birds"}

--> create a list of lists, where every item is {word 2 of file name, file}
repeat with i from 1 to count x
	set x's item i to {word 2 of getName(x's item i as text), x's item i}
end repeat

--> sort new list using anything able to sort list of lists
set x to Acme sort x --> eg, the scripting addition "ACME Script Widgets"

--> recompose list of files
repeat with i from 1 to count x
	set x's item i to x's item i's item 2
end repeat
x --> {alias "path:to:some birds", alias "path:to:some stuff", alias "path:to:some things"}

to getName(i)
	set AppleScript's text item delimiters to ":"
	set x to item -1 of i's text items
	set AppleScript's text item delimiters to {""}
	return x
end getName

jj, that’s very elegant except that OMM (Mac OS X 10.3.2/AS 1.9.3) the Acme sort from Acme Script Widgets.osax (3.0.3) causes Script Editor (2.0/v43) to quit. Is anyone else having problems with ASW.osax?

Thanks,
Jon

No problems here, same environment.

You can use this adaptation of one of Kai Edwards’ routines:

to sortLists from rList --> credits to Kai Edwards
	if rList's length < 2 then return rList
	set {l, h, {m, s}} to {{}, {}, rList's {item 1, rest}}
	repeat with r in s
		if m's item 1 > r's item 1 then
			set l's end to r's contents
		else
			set h's end to r's contents
		end if
	end repeat
	if l's length > 1 then set l to sortLists from l
	if h's length > 1 then set h to sortLists from h
	l & {m} & h
end sortLists

And replace “set x to Acme sort x” with “set x to sortLists from x”.

hey jj
it took me a little while to get it to work with the larger script it was part of but i finally did. maybe someday when i have a little time i’ll sit down and exactly figure out what’s actually going on in that sortlist subroutine. thanks a ton for your help. you’re a genius.