Sorting A List

Is there anyway to sort a list numerically or alphabetically? Whenever I set the contents of a folder into a list my list seems to be a random ordering of the folder items.

Mucho gracias


This is one of those questions that gets asked and answered hundreds of times a year. :wink:

When a problem seems like it might be an especially common one, it’s often a good idea to perform a search for it first, either with a mailing list’s search function:

or with a general internet search:

The alt.comp.lang.applescript newsgroup has a brilliant yet under-advertised faq:

The Finder under OS9 had a sort command, but it’s been disabled (never implemented) under OSX. You can obtain just the names of a folder’s contents with the “list folder” command from Standard Additions, which does in fact return the names in sorted order. It is more difficult to sort Finder objects or aliases, they need to be converted into pathstrings first:

on ListPathsInFolder(f)
	set astids to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "/:" -- won't occur in paths
		tell application "Finder" to ¬
			set str to every item of item f as string --> one big string
		set lst to str's text items
	on error m number n from f to t partial result p
		set AppleScript's text item delimiters to astids
		error m number n from f to t partial result p
	end try
	set AppleScript's text item delimiters to astids
	return lst
end ListPathsInFolder

set listOfPathStrings to ListPathsInFolder(choose folder)

You could now pass listOfStrings off to a sorting algorithm. Having sorted the list, you would then likely need to coerce the path strings back to aliases:

	set anAlias to alias listOfPathStrings's item 1

or back to Finder objects

	tell app "Finder"
		set aFinderRef to item listOfPathStrings's item 1

You could have just posted an answer to my question but since you didn’t here is my reply.

Your advice in regards to searching the web for answers to what is a common question instead of posting it here would be useful had the person posing the question (me) known said question was a common question. That being said you also made the assumption that I did not conduct a web search (I did, Google, AppleScript list sorting) and got many useless results. Remembering that macscripter is a location where people with questions regarding AppleScript’s could pose those questions I chose to come here as opposed to wasting my day hitting and missing with various search engines as, I think we can all agree the internet is not overflowing with AppleScript tutorials or examples and those that are out there tend to be pretty specific.

I also conducted a search here (Sorting a list) which yielded 1200+ results, of which non of the subject lines of the first fifty I looked at had subject lines that in any remote way indicated they would answer my question. I guess I could have spent time drilling through each result to see if any of these topics contained the answer I was looking for in the thread but that would have been a tedious process. For a common question that gets asked 100s of times one would think there would be more references to it here.

One would also think that because the question is a common one there are many others besides myself who would benefit from the answer and therefore I have performed a nice service for the AppleScript community by keeping the question percolating at the top of the heap as opposed to some of the more obscure issues that are raised here.

That being said, thanks for the reply, I haven’t tried it yet but I am sure it will be helpful.

It’s a good policy not to be rude to people who’ve gone to some length to help you. You might need more help in the future.

First, I would like to apologize. Upon rereading my posting, I realize that I may have come off as rude or at least patronizing. This was not my intention. I was trying to be helpful in as efficient a manner as possible.

But I did. I provided a link to another thread that very completely dealt with sorting lists, and then I posted a handler for listing files as sort-ready path strings. I don’t personally see the purpose in reposting a lengthy script when a link to an existing solution would suffice.

This point is one I have to strongly disagree with. Keeping common issues percolating at the top of the lists would only be useful to those who join this list for 1 or 2 days because they have a specific problem. It is far better to provide links to faqs, tutorials, and other established resources to those whose scripting needs have been asked and answered frequently, allowing the current threads of this forum to deal with the more difficult problems that arise.

Again, if I was rude, it was not intentional. I like helping people with their scripting needs, (that is why I’m on this list). :slight_smile:

Of course, being a fellow Philadelphian, we’re both quite used to rude, aren’t we? :wink: