Re: Sorting A List - Need Help
Re: Sorting A List - Need Help
- Subject: Re: Sorting A List - Need Help
- From: Nigel Garvey <email@hidden>
- Date: Sun, 27 Jul 2003 01:46:51 +0100
Rick Bargerhuff alias cougar wrote on Sat, 26 Jul 2003 16:28:05 -0400:
>
I am trying to sort a directory the way the Mac OS X Finder does it.
>
As you can see it sorts correctly. But if I add untitled folders...
>
like so...
>
untitled folder
>
untitled folder 10
>
untitled folder 11
>
untitled folder 12
>
untitled folder 13
>
untitled folder 14
>
untitled folder 15
>
untitled folder 16
>
untitled folder 17
>
untitled folder 18
>
untitled folder 19
>
untitled folder 2
>
untitled folder 3
>
untitled folder 4
>
untitled folder 5
>
untitled folder 6
>
untitled folder 7
>
untitled folder 8
>
untitled folder 9
>
zzz
>
The finder in Mac OS X
>
sorts these
>
file names correctly. Meaning 9 comes before 10...
>
>
I was looking for an applescript sorting algorithm, I dunno which to
>
try to port, to solve my problem.
I don't know if you'll find an existing algorithm for this. It's not so
much that the Finder sorts the names correctly, but that it displays them
conveniently in list view.
The following does what you want *provided that* the numbers come at the
ends of the names (followed or not by extensions), that they're preceded
by equal numbers of spaces (at least 1), and that they don't go higher
than 99. It works by temporarily changing returned names that end with
single-digits, resorting, and then restoring the original names.
-- QuickSort by Arthur J Knapp
on QuickSort(a, l, r) -- sorts in-place
local i, j, v
-- Much thanks to both Serge Belleudy-d'Espinose and Victor Yee
-- for the script-referencing techniques that they helped to
-- refine.
--
script o
property p : a
end script
set i to l
set j to r
set v to o's p's item ((l + r) div 2)
repeat while (j > i)
repeat while (o's p's item i < v)
set i to i + 1
end repeat
repeat while (o's p's item j > v)
set j to j - 1
end repeat
if (not i > j) then
set {o's p's item i, o's p's item j} to {o's p's item j, o's p's
item i}
set {i, j} to {i + 1, j - 1}
end if
end repeat
if (l < j) then QuickSort(o's p, l, j)
if (r > i) then QuickSort(o's p, i, r)
end QuickSort
tell application "Finder"
set filenames to name of (path to current user folder)'s folders --
change to 'files' in use
end tell
-- Merge the names into one, (ASCII character 1)-delimited string
set asc1 to ASCII character 1
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to asc1
set filenames to (filenames as string) & asc1
-- Replace all instances of space & single digit at the ends of names
-- with two spaces and that digit
repeat with i from 1 to 9
set AppleScript's text item delimiters to " " & i & asc1
set filenames to filenames's text items
set AppleScript's text item delimiters to " " & i & asc1
set filenames to filenames as string
set AppleScript's text item delimiters to " " & i & "."
set filenames to filenames's text items
set AppleScript's text item delimiters to " " & i & "."
set filenames to filenames as string
end repeat
-- Separate the names and resort them
set AppleScript's text item delimiters to asc1
-- (There's an ascii 1 on the end that's no longer needed)
set filenames to filenames's text items 1 thru -2
QuickSort(filenames, 1, count filenames)
-- Remerge the sorted names and single the double spaces
set filenames to filenames as string
set AppleScript's text item delimiters to " "
set filenames to filenames's text items
set AppleScript's text item delimiters to " "
set filenames to filenames as string
-- Spearate the names again into the required list
set AppleScript's text item delimiters to asc1
set filenames to filenames's text items
set AppleScript's text item delimiters to astid
filenames
NG
_______________________________________________
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.