In re-reading Kevin Bradley’s Sort Tutorial, I’ve decided to use his Bubble Sort handler in my iTunes script. I want to sort a few tracks based on part of its name. I figure I need the location, name and my character that I will parse out on its name. That character is what s/b sorted on in the list.
I’m stymied because how would the script “know” that the number “1” in the above is attached to its track location?
So I’m thinking I should create a record on the fly and sort based on that third field. Reading the forum makes me think that creating a record on the fly is not worth the effort. So how would I solve that problem?
tell application "iTunes"
get selection of front browser window
if (selection of front browser window) is not {} then
set sel to selection
repeat with aItem from 1 to count of sel
set x to item aItem of sel
set iname to name of x
set bNo to character -1 of iname as integer
return x
--set each x to a record that has its location and name and number from name.
--sort each record by the number in name. Then act on its location after sorting
end repeat
end if
--do more stuff that is working but out of scope for this post
end tell --itunes
------------handlers
on bubbleSort(theList)
-- defining an internal script makes for faster run times!
script bs
property alist : theList
end script
set theCount to length of bs's alist
if theCount < 2 then return bs's alist
repeat with indexA from theCount to 1 by -1
repeat with indexB from 1 to indexA - 1
if item indexB of bs's alist > item (indexB + 1) of bs's alist then
set temp to item indexB of bs's alist
set item indexB of bs's alist to item (indexB + 1) of bs's alist
set item (indexB + 1) of bs's alist to temp
end if
end repeat
end repeat
return bs's alist
end bubbleSort
thanx, sam
Browser: Safari 523.12.2
Operating System: Mac OS X (10.4)