Can someone post the script to sort list alphabetically. I lost my subroutine somehow. Stupid full moon or somethin’ like that.
Assuming that you mean AppleScript rather than shell script, this is the one that I use (From: http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.05.htm:
set the composer_list to {"Ellington", "Copland", "Bach", "Mozart"}
choose from list the reverse of ASCII_Sort(the composer_list)
--By default, the sub-routine returns the list in
--ascending order. If you wish to have the list read
--in descending order, use the reverse command:
--set the composer_list to {"Ellington", "Copland",
--"Bach", "Mozart"}
--choose from list the reverse of ASCII_Sort(the composer_list)
on ASCII_Sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end ASCII_Sort
Thanks a lot Adam!
Crud, that’s not exactly doing it for me. I think that’s my fault, my list is all numbers and symbols, the first half are dates, the second file sizes:
{“8/1/20051.607", "2/3/20050.15”, “2/2/20050.062", "1/28/20050.401”, “1/27/20050.312", "1/25/20050.206”, “1/14/20050.383", "1/12/20050.57”, “6/27/20050.268", "1/7/20050.159”, “1/6/20050.263", "7/7/20050.515”, “1/5/20050.122", "1/4/20050.068”, “1/3/20050.248", "7/14/20050.166”}
the sort provided kind of works, but my list ended up like 1/12…,1/14…,1/25…,1/27…,1/28…
I need to sort so the dates are in calendar order. Sorry for the confusion on that, I wasn’t thinking.
Are the asterisks you show just continuation symbols or are they really there? How is the date part separated from whatever is next in each string? Sorting that by date wouldn’t be too hard - it’s just a matter of grabbing the pieces using text item delimiters.
This uses a handler based on code by Nigel Garvey and puts your dates in ASCII sortable form: (I didn’t go any further because I didn’t know what you wanted to do)
set d to {"8/1/2005**1.607", "2/3/2005**0.15", "2/2/2005**0.062", "1/28/2005**0.401", "1/27/2005**0.312", "1/25/2005**0.206", "1/14/2005**0.383", "1/12/2005**0.57", "6/27/2005**0.268", "1/7/2005**0.159", "1/6/2005**0.263", "7/7/2005**0.515", "1/5/2005**0.122", "1/4/2005**0.068", "1/3/2005**0.248", "7/14/2005**0.166"}
set n to {}
set text item delimiters to "**"
repeat with p in d
set end of n to swapD(date (text item 1 of p))
end repeat
set text item delimiters to ""
n
to swapD(aDate)
tell {aDate}
copy beginning to end
set end's month to January
tell ((beginning's year) * 10000 + (beginning - (end - 3944592)) div 2629728 * 100 + (beginning's day)) as string
text 1 thru 8
end tell
end tell
end swapD
--> {"20050801", "20050203", "20050202", "20050128", "20050127", "20050125", "20050114", "20050112", "20050627", "20050107", "20050106", "20050707", "20050105", "20050104", "20050103", "20050714"}
Using AppleMods’List library:
property _Loader : run application (get "LoaderServer")
----------------------------------------------------------------------
-- DEPENDENCIES
property _List : missing value
on __load__(loader)
set _List to loader's loadLib("List")
end __load__
----------------------------------------------------------------------
-- MAIN CODE
__load__(_Loader's makeLoader()) -- load libraries
set lst to {"8/1/2005**1.607", "2/3/2005**0.15", "2/2/2005**0.062", "1/28/2005**0.401", "1/27/2005**0.312", "1/25/2005**0.206", "1/14/2005**0.383", "1/12/2005**0.57", "6/27/2005**0.268", "1/7/2005**0.159", "1/6/2005**0.263", "7/7/2005**0.515", "1/5/2005**0.122", "1/4/2005**0.068", "1/3/2005**0.248", "7/14/2005**0.166"}
script evalObj
property reverseSort : false
on eval(n)
set text item delimiters to "**"
return date (text item 1 of n)
end eval
end script
_List's powerSort(lst, {evalObj}, 0)
If you’ve not used AppleMods’ libraries before, you’ll need to download and install AppleMods’ Loader system first. Run the Loader installer, then download and add the List library to the ASLibraries folder. You can use the LoaderWizard applet to generate the library loading code to paste at the top of your script.
I’ll throw this out here.
set composers to {"Ellington", "Copland", "Bach", "Mozart"}
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 10}
set composers to composers as text
set AppleScript's text item delimiters to ASTID
do shell script "echo " & quoted form of composers & " | /usr/bin/sort"
set composers to paragraphs of result
To sort the list by date and by file size, you could also try something like this:
to sort_items from v
script l
property m : v
property n : m's items
end script
set c to count l's n
set tid to text item delimiters
set text item delimiters to "/"
repeat with i from 1 to c
tell l's n's item i to set l's n's item i to (10000 * (text item 3's text 1 thru 4) ¬
+ 100 * (text item 1) + (text item 2) as string) & text item 3's text 5 thru -1
end repeat
set text item delimiters to tid
considering case
repeat with i from 2 to count l's n
set v to l's n's item i
set w to l's m's item i
repeat with i from (i - 1) to 1 by -1
tell l's n's item i to if v < it then
set l's n's item (i + 1) to it
set l's m's item (i + 1) to l's m's item i
else
set l's n's item (i + 1) to v
set l's m's item (i + 1) to w
exit repeat
end if
end repeat
if i is 1 and v < l's n's beginning then
set l's n's item 1 to v
set l's m's item 1 to w
end if
end repeat
end considering
end sort_items
set l to {"8/1/2005**1.607", "2/3/2005**0.15", "2/2/2005**0.062", "1/28/2005**0.401", "1/27/2005**0.312", "1/25/2005**0.206", "1/14/2005**0.383", "1/12/2005**0.57", "6/27/2005**0.268", "1/7/2005**0.159", "1/6/2005**0.263", "7/7/2005**0.515", "1/5/2005**0.122", "1/4/2005**0.068", "1/3/2005**0.248", "7/14/2005**0.166"}
sort_items from l
l --> {"1/3/2005**0.248", "1/4/2005**0.068", "1/5/2005**0.122", "1/6/2005**0.263", "1/7/2005**0.159", "1/12/2005**0.57", "1/14/2005**0.383", "1/25/2005**0.206", "1/27/2005**0.312", "1/28/2005**0.401", "2/2/2005**0.062", "2/3/2005**0.15", "6/27/2005**0.268", "7/7/2005**0.515", "7/14/2005**0.166", "8/1/2005**1.607"}
Bruce, your quoted shell for list sorting, I’ve just made use of thank you. with the " -r" to reverse, do you find many instances where using a shell would be preferable to plain vanilla applescript? The other thing is are there any pitfalls when using shell, differences in the shell say between being run on panther or tiger boxes is the unix flavour common? My knowledge of unix is very limited so be pretty basic if possible?