• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Sort items in a list without OSAXen
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sort items in a list without OSAXen


  • Subject: Re: Sort items in a list without OSAXen
  • From: has <email@hidden>
  • Date: Sun, 2 Sep 2001 18:41:06 +0100

Joe,

Since you only need three numbers out of UnsortedList, you don't have
bother sorting it. That saves a bit of work right away.

Here's a handler that'll return the sum of the three highest numbers in
UnsortedList:


on getsumofhighestthree(UnsortedList)
set a to 0 --the lowest possible value in UnsortedList
set b to a
set c to a
repeat with eachitem in UnsortedList
if (eachitem > a) then
set c to b
set b to a
set a to eachitem
else if (eachitem > b) then
set c to b
set b to eachitem
else if (eachitem > c) then
set c to eachitem
end if
end repeat
return a + b + c
end getsumofhighestthree


It should be pretty efficient as it only goes through the UnsortedList
once. No fancy structures either.

You'll note it does make an assumption as to the lowest possible value in
UnsortedList (this was simplest). Change this value to some hugely negative
number if you're using any negative values in UnsortedList.

If you won't always be summing three numbers, however, you may be better
off sticking with a generic sort routine. Or you could do a more flexible
version of the above (I'm dunno how fast it'd run though).

HTH.

has

p.s. Out of interest - and this is for the more knowledgeable folks than me
- what would be the most efficient sort algorithm you could do in AS? And
how would it compare for speed against using an OSAX?


  • Follow-Ups:
    • Re: Sort items in a list without OSAXen
      • From: Emmanuel <email@hidden>
  • Prev by Date: Re: processing time & stack overflow!!!
  • Next by Date: Re: coercing a list
  • Previous by thread: Re: Sort items in a list without OSAXen
  • Next by thread: Re: Sort items in a list without OSAXen
  • Index(es):
    • Date
    • Thread