sort a list of items?

I’m trying to sort a list of folders. The folders themselves are already sorted at the Finder level but when my script returns the list they are returned in a seemingly random order – not by date, not by name. Suggestions?

script lines:
set source_dir to choose folder with prompt “Choose Source Folder to Search”
set folder_contents to (list folder source_dir invisibles “false” as text)

result:
{“2009_7_14”, “2009_8_14”, “2009_8_25”, “2009_5_27”, “2009_5_4”, “2009_5_19”, “2009_6_14”, “2009_7_25”, “2009_7_11”, “2009_5_9”, “2009_8_16”, “2009_6_22”, “2009_7_20”, “2009_6_13”, “2009_7_6”, “2009_8_7”, “2009_8_19”, “2009_6_29”, “2009_7_30”, “2009_6_18”, “2009_7_19”, “2009_5_21”, “2009_6_16”, “2009_5_7”, “2009_7_9”, “2009_7_29”, “2009_8_13”, “2009_8_28”, “2009_6_19”, “2009_6_3”, “2009_8_12”, “2009_7_31”, “2009_6_20”, “2009_5_20”, “2009_8_31”, “2009_6_17”, “2009_6_30”, “2009_6_10”, “2009_6_1”, “2009_6_2”, “2009_5_28”, “2009_7_17”, “2009_7_8”, “2009_5_1”, “2009_6_28”, “2009_5_13”, “2009_8_8”, “2009_5_12”, “2009_6_15”, “2009_9_1”, “2009_8_9”, “2009_5_6”, “2009_8_21”, “2009_7_21”, “2009_6_25”, “2009_5_16”, “2009_7_3”, “2009_6_11”, “2009_8_11”, “2009_8_6”, “2009_7_2”, “2009_6_6”, “2009_8_20”, “2009_7_16”, “2009_6_23”, “2009_6_26”, “2009_5_22”, “2009_5_23”, “2009_5_29”, “2009_6_7”, “2009_7_24”, “2009_8_2”, “2009_5_11”, “2009_7_7”, “2009_5_15”, “2009_6_21”, “2009_7_27”, “2009_8_15”, “2009_7_28”, “2009_6_27”, “2009_8_30”, “2009_7_1”, “2009_6_24”, “2009_6_12”, “2009_6_9”, “2009_5_26”, “2009_8_18”, “2009_5_5”, “2009_8_29”, “2009_8_27”, “2009_8_24”, “2009_8_4”, “2009_5_8”, “2009_6_8”, “2009_7_23”, “2009_8_10”, “2009_8_23”, “2009_5_18”, “2009_5_30”, “2009_6_4”, “2009_7_26”, “2009_7_13”, “2009_5_14”, “2009_8_3”, “2009_6_5”, “2009_7_18”, “2009_8_17”, “2009_7_15”, “2009_8_22”, “2009_7_22”, “2009_7_5”, “2009_8_5”, “2009_7_10”, “2009_8_1”, “2009_9_2”, “2009_8_26”, “2009_7_12”}

Use the FInder’s sort function to sort the file references, and then do a quick repeat loop to create a final list of the file names of that sorted list of file references:

set source_dir to choose folder with prompt "Choose Source Folder to Search"

tell application "Finder"
	set folder_contents_temp to every file of source_dir
	set folder_contents_temp to (sort folder_contents_temp by name)
	set folder_contents to {}
	repeat with oneFile in folder_contents_temp
		set end of folder_contents to name of oneFile
	end repeat
end tell

folder_contents

Well, that’s just it. I’m trying to avoid sorting the items at the finder level since source_dir exists on a SMB mounted server and the folder contents are quite large. Thus, it becomes quite time consuming. Is there no way to sort the list within my script without impacting the Finder?

after further testing, your advice works as long as source_dir is local. If it’s on the SMB mount, it locks up my finder. I also forgot to mention that what I’m trying to sort are subfolders, which behaves differently. For example, I had to change “every file” to “every folder”. That’s when it locks up on a SMB mount. Any thoughts?

tell application “Finder”
set folder_contents_temp to every folder of source_dir
set folder_contents_temp to (sort folder_contents_temp by name)
set folder_contents to {}
repeat with oneFile in folder_contents_temp
set end of folder_contents to name of oneFile
end repeat
end tell

folder_contents

Here’s a couple of things to try. Sorting is not something the computer does efficiently so creating manual ways of doing it might actually take longer. Try the unix approach first. That might be quick enough.

Unix/Shell script:

set source_dir to choose folder with prompt "Choose Source Folder to Search"
set folder_contents to (list folder source_dir invisibles "false" as text)

set text item delimiters of AppleScript to ASCII character 10
set folder_contents to "" & folder_contents
set text item delimiters of AppleScript to ""

do shell script "echo " & quoted form of folder_contents & " | /usr/bin/sort"
set folder_contents to paragraphs of result

A sort function:

set source_dir to choose folder with prompt "Choose Source Folder to Search"
set folder_contents to bubbleSortList(list folder source_dir invisibles "false" as text)

on bubbleSortList(theList2Sort)
	repeat with i from length of theList2Sort to 2 by -1
		repeat with j from 1 to i - 1
			tell theList2Sort
				if item j > (item (j + 1)) then
					set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
				end if
			end tell
		end repeat
	end repeat
	return theList2Sort
end bubbleSortList

Model: iMac Intel 10.5.8
Browser: Safari 530.19
Operating System: Mac OS X (10.5)

Thanks this worked…

set source_dir to choose folder with prompt “Choose Source Folder to Search”
set folder_contents to (list folder source_dir invisibles “false” as text)

set text item delimiters of AppleScript to ASCII character 10
set folder_contents to “” & folder_contents
set text item delimiters of AppleScript to “”

do shell script “echo " & quoted form of folder_contents & " | /usr/bin/sort”
set folder_contents to paragraphs of result

…with a bit of modification to use the results as I needed. Basically, I had to reset the text item dleimiter to (return) in order to then get each item of folder_contents.

BTW – my end goal is/was to do a recursive search of a folder. (pick a source directory, then search that folder and any sub-folder within). The only way i could figure out how to do that was this convoluted method of getting the folder contents, sorting the list, and setting up a repeat loop to search the contents of each subfolder. Is/was there a simpler way?