Re: getting a file list in sorted order
Re: getting a file list in sorted order
- Subject: Re: getting a file list in sorted order
- From: has <email@hidden>
- Date: Sun, 18 Aug 2002 16:28:13 +0100
Some doofus wrote:
>
Alternatively, it goes faster if you work with an alias list instead:
>
>
======================================================================
>
>
--TEST
>
>
script evalObj
>
on isLessThan(val1, val2)
>
return (val1 as string < val2 as string)
>
end isLessThan
>
end script
>
>
tell application "Finder" to set theList to files of startup disk as
>
[NO-BREAK]alias list
>
quickSort(theList, evalObj)
>
>
======================================================================
Zzzzz... asleep on the job there. The above will mess up comparisons
between file paths and folder paths due to the extra colon on folder paths;
the following will remedy this:
======================================================================
script evalObj
on isLessThan(val1, val2)
set val1 to val1 as string
if val1's last character is ":" then set val1 to val1's text 1
[NO-BREAK]thru -2
set val2 to val2 as string
if val2's last character is ":" then set val2 to val2's text 1
[NO-BREAK]thru -2
return (val1 < val2)
end isLessThan
end script
tell application "Finder" to set theList to items of startup disk as
[NO-BREAK]alias list
quickSort(theList, evalObj)
======================================================================
No doubt this'll bog down performance some, but unless you're comparing all
files or all folders only then it can't really be helped.
Also, the quickSort may break because I originally used 'result' to scrape
some very marginal speed improvement; then later clashed in the support for
callbacks while forgetting that result is globally, not locally, scoped
(thus the callback handler may interfere with the qsort handler):
get its kList's item x
if evalObj's isLessThan(result, thePivot) then --may change result!
set its lesserList's end to result
(Must take more care with the little horror in future.) Again, here's the
fixed version:
======================================================================
on _quickSort(theList, evalObj)
script kludge
property kList : theList
property lesserList : {}
property greaterList : {}
end script
tell kludge
set listLength to (count its kList)
set midpointIndex to (listLength + 1) div 2
set thePivot to its kList's item midpointIndex
repeat with x from 1 to (midpointIndex - 1)
set theItem to its kList's item x
if evalObj's isLessThan(theItem, thePivot) then
set its lesserList's end to theItem
else
set its greaterList's end to theItem
end if
set theItem to its kList's item -x
if evalObj's isLessThan(theItem, thePivot) then
set its lesserList's end to theItem
else
set its greaterList's end to theItem
end if
end repeat
if (listLength - midpointIndex) is midpointIndex then
set theItem to its kList's item -midpointIndex
if evalObj's isLessThan(theItem, thePivot) then
set its lesserList's end to theItem
else
set its greaterList's end to theItem
end if
end if
if (count its lesserList) is greater than 1 then set its
[NO-BREAK]lesserList to my _quickSort(its lesserList, evalObj)
if (count its greaterList) is greater than 1 then set its
[NO-BREAK]greaterList to my _quickSort(its greaterList, evalObj)
return its lesserList & {thePivot} & its greaterList
end tell
end _quickSort
on quickSort(theList, evalObj)
if theList's class is not list then error "Not a list."
if (count theList) is greater than 1 then
return _quickSort(theList, evalObj)
else
return theList
end if
end quickSort
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
Such thrilling stuff.
has
--
(My email address has changed from <email@hidden> to
<email@hidden>. Please update your address books accordingly.)
_______________________________________________
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.