Bubble Sort on record field?

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)

Perhaps I’m not clear. Instead of a record, I’ll try sorting a list of a list based on the first item of the second/inner list:


	tell application "iTunes"
		set mySet to {}
		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 (location of track which I need)
				--return iname ("name of track Hour 3"
--return bNo ("3")
				set myRecord to {bNo, x, iname}
				copy myRecord to end of mySet
--return myRecord (This works)
				
			end repeat
--here's where I'm stuck. How do I sort on an item in the inner list? I want the items in the outer list be be acted on.
			my bubbleSort(theList)
			return mySet
end if -- sel not empty
end tell -- ituens
--Go on to do stuff based on the sort

So if you were me, how would you sort based on the number you find when you extract the name from the track id?

thanx, sam

I’m still hoping for help on this one.

Using Kevin’s BubbleSort, which expects a list, I don’t know how it would sort the results of this:


	tell application "iTunes"
		set mySet to {}
		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
set myRecord to {bNo, x, iname}
				copy myRecord to end of mySet
				
			end repeat
			return mySet
end tell --iTUens
BubbleSort(mySet)

So the result would be this, which looks like a record.
{(3, “location id 123”, “Number 3”), (2, “location id 144”, “Number 2”), (1, “location id 166”, “Number 1”)}

How would a sort happen on just the first item of each item in this list?

I’m stumped, thanx, sam