‘name’ is a term common to lots of things. When you use it in a script, it’s evaluated in context. So in your second script, sortOrder gets set to “Finder”, in the third it’s set to the name of the script. The only way I can see to do what you want is to set sortOrder to a reference to ‘name’ in a Finder context. This sets the variable to the Finder’s enum for ‘name’ instead of an actual name:
tell application "Finder" to set sortOrder to a reference to name
tell application "Finder"
set sortedFiles to sort selection by sortOrder
end tell
Your question is answered, but as I see, you want to provide to user choose sort order method from list. To do that you should map sort properties to its names (string representation, as choose from list works with simple AppleScript values):
set aChoice to choose from list {"Name", "Kind", "Creation Date", "Modification Date", "Locked", "Name Extension", "Owner", "Group", "Size"} with prompt "Choose Method to Sort By"
tell application "Finder"
if aChoice is "Name" then
sort selection by name
else if aChoice is "Kind" then
sort selection by kind
else if aChoice is "Creation Date" then
sort selection by creation date
else if aChoice is "Modification Date" then
sort selection by modification date
else if aChoice is "Locked" then
sort selection by locked
else if aChoice is "Name Extension" then
sort selection by name extension
else if aChoice is "Owner" then
sort selection by owner
else if aChoice is "Group" then
sort selection by group
else
sort selection by size
end if
end tell
NOTE: exists many other properties to sort items of selection. See ITEM properties in the Finder’s dictionary.