function for greatest object in list less than or equal to a value

dunbarx at dunbarx at
Mon Oct 12 13:56:14 EDT 2015


All you need is a button:

on mouseUp
   put "1,2,4,3,5,6,7,8,9,34," into temp
   repeat 16
      put temp after temp
   end repeat
   put the ticks into xx
   put  "Repeat:" && getMaxLessThan(temp,"5") into repeatResults
   put "  :Ticks:" && the ticks - xx  after repeatResults
   put the ticks into xx
   put  "Sort:" && findItemLessThanIndex(temp,"5") into sortResults
   put "  :Ticks:" && the ticks - xx  after sortResults
   answer repeatResults & return & sortResults
end mouseUp

function getMaxLessThan tList,maxVal 
   repeat for each item i in tList 
      if i < maxVal then put i & comma after outList 
   end repeat 
   return max(item 1 to -1 of outList) 
end getMaxLessThan 

function findItemLessThanIndex tData,tIndex
   put comma & tIndex after tData
   sort items of tData numeric
   return  item itemOffset("5",tData) -1 of tData
end findItemLessThanIndex

"Sort" is consistently faster, up to 50%. Why "up to"? Try it several times. As per the other, newer part of this thread, timing based on such things as "ticks" needs to be run many times to get an accurate "average" reading. Other system processes come into play, as well as gremlins.


-----Original Message-----
From: Peter TB Brett <peter.brett at>
To: How to use LiveCode <use-livecode at>
Sent: Mon, Oct 12, 2015 11:23 am
Subject: Re: function for greatest object in list less than or equal to a value

On 12/10/2015 15:24, dunbarx at wrote:
> I made a test between the
"sort" method and the "repeat for each". For a list of about 100,000 items, sort
finds the max value in 33 ticks, and "repeat" takes 50 ticks.
> This seems
logical to me, since lower level operations are used in "sort" as opposed to
"repeat" even though we all know how fast "repeat for each" is.

really surprising!  Could you please share your test stack so I 
can have a


Dr Peter Brett
<peter.brett at>
LiveCode Open Source Team

LiveCode on reddit:

mailing list
use-livecode at
Please visit this url to subscribe,
unsubscribe and manage your subscription


More information about the use-livecode mailing list