Probably a pointless question about using sort to search..

Mike Bonner bonnmike at gmail.com
Wed Dec 9 10:56:50 EST 2015


I was just thinking how useful sort could be to find things (in an array or
whatever)

I took the example from the other (word sort) thread and did this..
(obviously it would make more sense to NOT be building the array on the fly
and use it on an existing one.)

local sDataArray
on mouseUp
   put "   the    quick   brown fox  jumped" into tDat
   put empty
   put 0 into tCount
   repeat for each word  tWord in tDat
      add 1 to tCount

      put tWord into sDataArray[tCount]
   end repeat
   put the keys of sDataArray into tKeys
   set the foundit of me to 0
   sort lines of tKeys  by wordFunc(each, "u")
   if the foundit of me is not 0 then
      put line 1 to (the foundit of me) of tKeys
   end if
end mouseUp

function wordFunc pSortKey, pSearch
   if sDataArray[pSortKey] contains pSearch then
      set the foundit of me to the foundit of me + 1
      return 1
   else
      return 2
   end if
end wordFunc

The end result is a sorted list of keys, where all of the found items are
at the beginning of the keylist, and a property telling you how many of
those lines were the ones found.

While its interesting, i'm wondering if there might be applications where
this might actually be useful vs a simple repeat for each loop to pull out
the information directly.



More information about the use-livecode mailing list