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 18:43:30 +0100
Rick Bargerhuff alias cougar wrote on Sat, 26 Jul 2003 19:36:34 -0400:
>
> set my text item delimiters to ASCII character 10
>
> set s to ls as Unicode text
>
> set my text item delimiters to ""
>
>
>
> do shell script "perl -e '
>
> for (split qq~\\012~, qq~" & s & "~) {
>
> s~\\d+~sprintf(\"d\",$&)~e;
>
> push @ls2, $_}
>
> for ( sort @ls2) {
>
> s~ (0+)([1-9]+)~ $2~ ;
>
> print qq~$_$/~};'"
>
Ok, this is a special case and would work if the file names were named
>
using the same scheme...
>
But my scenario would be global. Meaning I don't really know what the
>
naming scheme would be.
>
I need to somehow reproduce that sorting algorithm the Finder uses. It
>
only seems right to mimic the Mac
>
OS X Finder's way of sorting.
I receive the digest, so I don't know if anyone's yet come up with the
definitive Unix or Perl solution for you. Here's an AppleScript one that
Quicksorts doctored versions of the file names - in which all the
"numbers" have had 10000 added to them. After the sort, the original
names are reinstated. It's long, but not particularly slow.
(* QuickSort handler adapted from Arthur Knapp *)
on QuickSortOnItem(a, l, r, n) -- sorts in-place
-- Quicksorts a list of lists, sorting on item n of each list
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)'s item n
repeat while (j > i)
repeat while (o's p's item i's item n < v)
set i to i + 1
end repeat
repeat while (o's p's item j's item n > v)
set j to j - 1
end repeat
if j is not less than i then
tell o's p's item i
set o's p's item i to o's p's item j
set o's p's item j to it
end tell
set i to i + 1
set j to j - 1
end if
end repeat
if (l < j) then QuickSortOnItem(o's p, l, j, n)
if (r > i) then QuickSortOnItem(o's p, i, r, n)
end QuickSortOnItem
on FinderDisplaySort(itemNames)
-- Replace each name in the list with a sublist containing a
-- (possibly) doctored name for sorting and the orginal name
considering case
repeat with thisName in itemNames
if thisName contains "1" or thisName contains "2" or thisName
contains "3" or thisName contains "4" or thisName contains "5" or
thisName contains "6" or thisName contains "7" or thisName contains "8"
or thisName contains "9" or thisName contains "0" then
-- If this name contains a number, get a version bracketed with
"X"'s
-- (Knowing that it doesn't begin or end with a number saves a
few checks)
set Xname to "X" & thisName & "X"
set Xlen to (count Xname)
-- Make up a sorting name in which the original name's numeric
-- portions have 10000 added
set sortName to ""
set r to 1
repeat
set l to r
repeat until r > Xlen or character r of Xname is in
"1234567890"
set r to r + 1
end repeat
set sortName to sortName & text l thru (r - 1) of Xname
if r > Xlen then exit repeat
set l to r
repeat while character r of Xname is in "1234567890"
set r to r + 1
end repeat
set sortName to sortName & ((text l thru (r - 1) of Xname) +
10000)
end repeat
-- Relace the original name in the list with a sublist
containing the
-- sorting name and the original name
set thisName's contents to {text 2 thru -2 of sortName,
thisName's contents}
else
-- If the name doesn't contain a number, the sorting name's the
same
-- as the original
set thisName's contents to {thisName's contents, thisName's
contents}
end if
-- The Finder sorts "_" as " ", so modify the sorting name if
necessary
if thisName's item 1 contains "_" then
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "_"
set theBits to thisName's item 1's text items
set AppleScript's text item delimiters to " "
set thisName's item 1 to theBits as string
set AppleScript's text item delimiters to astid
end if
end repeat
end considering
-- Sort the sublists on the first (sorting) name in each
QuickSortOnItem(itemNames, 1, count itemNames, 1)
-- Substitute the original names for the sorted sublists
repeat with thisName in itemNames
set thisName's contents to item 2 of thisName
end repeat
return itemNames
end FinderDisplaySort
get path to current user folder
tell application "Finder"
set theNames to (name of every item of result) as list
end tell
choose from list (FinderDisplaySort(theNames))
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.