Re: Sort a list of lists
Re: Sort a list of lists
- Subject: Re: Sort a list of lists
- From: CYB <email@hidden>
- Date: Thu, 16 Nov 2006 19:51:00 -0600
- Thread-topic: Sort a list of lists
Title: Re: Sort a list of lists
Many many thanks Andrew for your code
Also thanks Shane, it’s a good way
--
Carlos Ysunza
Director
SoftRobot Automated Workflows
http://www.softrobot.com.mx
email@hidden
From: Andrew Oliver <email@hidden>
Date: Thu, 16 Nov 2006 17:34:42 -0800
To: CYB <email@hidden>
Cc: applescript-users <email@hidden>
Subject: Re: Sort a list of lists
On Nov 16, 2006, at 2:59 PM, CYB wrote:
Thanks Andrew, wonderful idea
I’ll go thru this way, but now I have some question that I forgot to ask in my first post
I need to know to what item list belongs the smallest number, in fact to what item list belongs each one
Any ideas?
Hopefully this will give you some pointers.
Given a list of coordinates ('coords') this script shows you ways of a) finding their mean squares, b) finding the closest to top-left, c) finding the furthest from top-left, and d) sorting them in order of closest to furthest:
global coords, squares
set coords to {{5.0, 120}, {10.0, 10.0}, {70.0, 20.0}, {70.0, 80.0}}
set squares to {}
repeat with eachCoord in coords
copy (getSquared(eachCoord)) to end of squares
end repeat
-- so now you have a list of coordinates (coords) and their squared values (squares)
set minItem to findMin() -- find the smallest (closest to top-left)
set maxItem to findMax() -- find the largest (furthest from top-left)
sortCoords() -- sort them. not needed here, just for demo
on findMin()
set min to 1
repeat with i from 2 to (count squares)
if item i of squares < item min of squares then
set min to i
end if
end repeat
return min -- the index to the closest item
end findMin
on findMax()
set max to 1
repeat with i from 2 to (count squares)
if item i of squares > item max of squares then
set max to i
end if
end repeat
return max -- the index to the furthest item
end findMax
on sortCoords()
-- use a double list bubble sort (although any sort algorithm will work)
-- iterate through one list (squares). For each swap, swap the corrsponding items in the other list
set sorted to false
repeat until sorted is true
set sorted to true
repeat with i from 2 to (count coords)
if item (i - 1) of squares > (item i of squares) then
set tempOutput to item (i - 1) of squares
set tempInput to item (i - 1) of coords
set item (i - 1) of squares to item i of squares
set item (i - 1) of coords to item i of coords
set item i of squares to tempOutput
set item i of coords to tempInput
set sorted to false
end if
end repeat
end repeat
end sortCoords
on getSquared(inputVals)
set outputVal to 0
repeat with eachVal in inputVals
set outputVal to outputVal + (eachVal ^ 2)
end repeat
return outputVal / (count inputVals)
end getSquared
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/mailman//archives/applescript-users
This email sent to email@hidden