• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: sort order
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sort order


  • Subject: Re: sort order
  • From: Graff <email@hidden>
  • Date: Wed, 29 Sep 2004 13:24:21 -0400

No, the items will often not come back to you in alphabetical order. You'll have to roll your own sorting algorithm and sort the items yourself.

I believe that the sort command doesn't work even in Panther. Here is what it says in the Finder dictionary:
"sort: (NOT AVAILABLE YET) Return the specified object(s) in a sorted list"


Here is a sort handler that takes in an unsorted list of file objects and returns a sorted one:
----
on SortFiles(unsortedList)
tell application "Finder"
set sortedList to {item 1 of unsortedList}
repeat with i from 2 to (count of unsortedList)
set isPlaced to false
repeat with j from 1 to (count of sortedList)
ignoring case and diacriticals
if (name of item i of unsortedList < name of (item j of sortedList)) then
if (j = 1) then
set sortedList to {(item i of unsortedList)} & sortedList
else
set sortedList to (items 1 thru (j - 1) of sortedList) & {item i of unsortedList} & items j thru -1 of sortedList
end if
set isPlaced to true
exit repeat
end if
end ignoring
end repeat
if (not isPlaced) then
set sortedList to sortedList & {item i of unsortedList}
end if
end repeat
end tell


  return sortedList
end SortFiles

set theSorted to SortFiles(choose file with multiple selections allowed)
set theText to ""
tell application "Finder"
  repeat with i in theSorted
    set theText to theText & name of i & return
  end repeat
end tell

display dialog theText
----

Note that this sorts strictly on AppleScript's string comparison so it won't sort exactly like the Finder does. The main difference is how the Finder sorts files with numbers at the end. The Finder will sort "name02" after "name1", whereas this handler will sort them as "name02", "name1" because lexicographically the character "0" comes before the character "1". This can be fixed by padding all the trailing numbers in names with zeros so that they all have the same number of digits, for example: "name1" would become "name01" and then it would sort as "name01", "name02". Doing this is left to the reader...

And yes, I could have done the insertions as part of a binary search but I was too lazy to code it. Someone else can do it if they are inspired! :-)

- Ken

On Sep 29, 2004, at 11:10 AM, Preston Smith wrote:

Jaguar is missing the sort command for finder.

When using Jaguar, can I assume that if I list all items in a folder that they will always come back in alphabetical (name) order?

_______________________________________________ Do not post admin requests to the list. They will be ignored. Applescript-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >sort order (From: Preston Smith <email@hidden>)

  • Prev by Date: Re: How to parse a textfile ?
  • Next by Date: Re: ftp transfer
  • Previous by thread: sort order
  • Next by thread: Re: sort order
  • Index(es):
    • Date
    • Thread