Re: Sort items in a list without OSAXen
Re: Sort items in a list without OSAXen
- Subject: Re: Sort items in a list without OSAXen
- From: Timothy Bates <email@hidden>
- Date: Sun, 02 Sep 2001 16:05:15 +1000
I noted that you don't need to sort the list - just keep items that are
larger than the best three candidates so far. so this works pretty fast.
make sure to initalise the candidates to the lowest number you will
experience.
set theList to {}
repeat with n from 1 to 100
set the end of theList to random number from 1 to 500
end repeat
set candidates to {0, 0, 0}
repeat with anItem in theList
if anItem > item 1 of candidates then
set candidates to {contents of anItem, item 1 of candidates, item 2
of candidates}
else if anItem > item 2 of candidates then
set candidates to {item 1 of candidates, contents of anItem, item 2
of candidates}
else if anItem > item 3 of candidates then
set item 3 of candidates to contents of anItem
end if
--display dialog ("" & anItem & return & (join list candidates with
delimiters " "))
end repeat
{candidates, theList}
On 9/2/01 2:54 PM, "Joseph A. Weaks" <email@hidden> wrote:
>
I have a variable that contains random numbers in a list, say
>
{1,2,3,4}, and I need the sum of the highest three.
>
I don't want to use an OSAX 'sort' command for portability reasons.
>
My approach has been to first sort the list in descending in order
>
which lets me get the sum items 1-3. Here's my solution so far...
>
>
>
-- Sorts a list in descending order
>
property HighNumber : 10 -- Highest possible number
>
property SortedList : {} as list
>
>
SortList({9, 1, 3, 4, 10, 6, 5, 7, 2, 1, 1})
>
>
on SortList(UnsortedList)
>
repeat with EachNumber from HighNumber to 1 by -1
>
repeat with x from 1 to number of UnsortedList
>
if item x of UnsortedList is EachNumber then
>
set SortedList to SortedList & item x
>
of UnsortedList as list
>
end if
>
end repeat
>
end repeat
>
return SortedList
>
end SortList
>
>
>
>
>
Any more economical way?
>
>
Joe Weaks
Dr Timothy Bates <
mailto:email@hidden>
Macquarie Centre for Cognitive Science (MACCS)
Macquarie University
Ph 61 (2) 9850 8623
Fx 61 (2) 9850 6059