• 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: how to sort a list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to sort a list


  • Subject: Re: how to sort a list
  • From: Christopher Nebel <email@hidden>
  • Date: Thu, 13 May 2004 17:26:52 -0700

On May 13, 2004, at 2:23 PM, eric m katerman wrote:

how to sort a list?

set the_list to {"c", "b", "e", "a", "d"}
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"
"}
set list_string to (the_list as string)
do shell script "echo \"" & list_string & "\" > /Users/eric/data/tmp.dat"
do shell script "sort /Users/eric/data/tmp.dat > /Users/eric/data/tmp2.dat"
set new_string to (do shell script "cat /Users/eric/data/tmp2.dat")
set new_list to (words of new_string)

Not bad, but it has several obvious problems:

- Won't work as typed in all editors, because the delimiter must be a linefeed, not a carriage return.
- Will break if the input contains double quotes.
- Uses a temporary file (which it doesn't clean up), when it could just use pipelining.
- Sorts in ASCII order, which makes all uppercase letters come before all lowercase ones.
- Assumes that each item is a single word; multi-word items will turn into separate items in the result.
- Saves the old delimiters (good) but doesn't restore them (bad).

Here's an improved version of the same basic script:

set the_list to {"c", "b", "e", "a", "d"}
set old_delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 10} -- always a linefeed.
set list_string to (the_list as string)
set new_string to do shell script "echo " & quoted form of list_string & " | sort -f"
-- fixes quoting and temporary file; -f option sorts upper- and lower-case together.
set new_list to (paragraphs of new_string) -- paragraphs, not words.
set AppleScript's text item delimiters to old_delims -- restore delimiters.
return new_list

Of course, this will produce utterly not what you want for numbers, but there are ways to tell sort(1) to do the right thing. It's possible to sort the list without resorting to "do shell script", but it's more code. Fortunately, you don't have to write it yourself, since there are several public sorting routines floating around. See <http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.05.htm> for one of them.


--Chris Nebel
AppleScript Engineering
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.


References: 
 >how to sort a list (From: Andre Vink <email@hidden>)
 >Re: how to sort a list (From: eric m katerman <email@hidden>)

  • Prev by Date: Re: Getting values from keychain
  • Next by Date: Re: Applescript bundle droplet problems
  • Previous by thread: Re: how to sort a list
  • Next by thread: Re: how to sort a list
  • Index(es):
    • Date
    • Thread