AppleScript Text Sort via Unix

Sometimes you need to sort a list before presenting it to the user via “choose from list.” This is particularly important when working with Helix RADE, as the list of icons is sorted by selection order, not by name.

While various methods have been put forward, none rival the speed of the Unix ‘sort’ command. The following code is modified from a hint found at

on sortItems(itemList)
      -- modded from
   local listString, newString, newList
   set oldDelims to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {ASCII character 10} -- always a linefeed
   set listString to (itemList as string)
   set newString to do shell script "echo " & ¬
      quoted form of listString & " | sort -g" -- add -r for reverse
   set newList to (paragraphs of newString)
   set AppleScript's text item delimiters to oldDelims
   return newList
end sortItems