How do I sort a list of File paths?

G’day

I’d like to be able to sort a list of selected files alphabetically, but it’s beyond me.

It was simple in Hypercard because of it’s ‘short name’ feature.

I’ve tried this…

tell application "Finder"
	try
		set thelist to (selection as list)
		set xx to length of thelist
		
		repeat
			repeat with x from 1 to xx - 1
				set firstGo to item x of thelist
				set SecondGo to item (x + 1) of thelist
				-- this bit of course, won't work
				if firstGo > SecondGo then
					set item x of the list to SecondGo
					set item (x + 1) of the list to firstGo
				end if
			end repeat
			set xx to xx - 1
			if xx = 1 then exit repeat
		end repeat
	end try
end tell

which naturally won’t sort. Any help appreciated.

Regards, Santa

It’s ok, I solved it with this…


tell application "Finder"
	try
		set thelist to (selection as list)
		set xx to length of thelist
		ignoring case, punctuation and white space
			repeat
				repeat with x from 1 to xx - 1
					set firstGo to name of item x of thelist as string
					set SecondGo to name of item (x + 1) of thelist as string
					if (firstGo > SecondGo) then
						set swapthis to item x of thelist
						set item x of thelist to item (x + 1) of thelist
						set item (x + 1) of thelist to swapthis
					end if
				end repeat
				set xx to xx - 1
				if xx = 1 then exit repeat
			end repeat
		end ignoring
	end try
end tell

Thank anyway.:slight_smile:

Hi, Santa.

The Finder’s sort command was reinstated as from Tiger.

tell application "Finder"
	set theSelection to selection
	set theList to (sort theSelection by name)
end tell

Like the Finder’s selection, it returns a list of Finder items, not paths. It also has the peculiarity that numeric portions of names are sorted numerically and then subsorted lexically, rather than being sorted purely lexically. So if you’ve selected four files called, say, “m1958.jpg”, “m1959.jpg”, “m01959.jpg”, and “m001959.jpg”, the sorted order is:

m1958.jpg
m01959.jpg
m001959.jpg
m1959.jpg

Not:

m01959.jpg
m001959.jpg
m1958.jpg
m1959.jpg

This shouldn’t normally be a problem.

Ahh, I didn’t know it was back. :confused:

Thanks