Probably a pointless question about using sort to search..

Peter M. Brigham pmbrig at gmail.com
Sat Dec 12 10:21:31 EST 2015


At this point probably beating a dead horse, but here's a cleaned up handler that handles cr's in the data. Geoff is right that the basic idea can be done in 3 lines, but this automates choosing an alternate delimiter and specifying the sort mode in a one-line call.

The original handler came from one of the LC lessons, and was used to prepare an array for a function to search the contents of an array that scales logarithmically rather than linearly.

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig

-------

on sortArray @pArray, pSortMode, pDelim
   -- sort the values of an array, returning an integer-keyed array
   --   whose values increase as the keys increase
   -- pArray is referenced, so the original array will be changed
   --   note that the original keys of the array will be discarded,
   --   thus most useful for transforming numerically-keyed arrays
   -- pSortMode = {"text"|"textCaseSens"|"numeric"|"dateTime"}
   --    textCaseSens sets caseSensitive to true before sorting by text
   --    if sortMode = empty, defaults to numeric
   -- specify a delimiter pDelim if the elements of the array contain cr's
   -- you can use this to prepare an array for the logarithmicSearch() function
   -- not sure who wrote the original,
   --    cleaned up and expanded by Peter M. Brigham, pmbrig at gmail.com,
   --    with help from the use-LC list
   
   if pSortMode = empty then put "numeric" into pSortMode
   if pDelim = empty then put cr into pDelim
   combine pArray with pDelim
   -- now pArray is a pDelim-delimited list of values
   -- sort the list as directed
   set the linedel to pDelim
   switch pSortMode
      case "textCaseSens"
         set the casesensitive to true
      case "text"
         sort lines of pArray
         break
      case "numeric"
         sort lines of pArray numeric
         break
      case "dateTime"
         sort lines of pArray dateTime
         break
   end switch
   # transform the sorted list back to a numerically indexed array
   split pArray with pDelim
end sortArray


On Dec 11, 2015, at 7:34 PM, Geoff Canyon wrote:

> On Fri, Dec 11, 2015 at 6:51 PM, Alex Tweedly <alex at tweedly.org> wrote:
> 
>> 
>> I haven't read everything in this thread, so I don't know if Peter's
>> function handled this or not ...
>> 
>> On 11/12/2015 23:32, Geoff Canyon wrote:
>> 
>>> On Thu, Dec 10, 2015 at 12:47 PM, Peter Brigham <pmbrig at gmail.com> wrote:
>>> 
>>>> 
>>>> But back to the first point, this function can be eliminated in three
>>> lines
>>> of code:
>>> 
>>> combine someArray using cr
>>> sort lines of someArray -- however you like
>>> split someArray by cr
>>> 
>>> Ummm ...
>>   put "abc" & CR & "def" into somearray[1]
>>   put "ghj" into somearray[2]
>> and then try the above 3-line code snippet.
>> 
>> -- Alex.
> 
> 
> From the function:
> 
> repeat for each line k in keyList
>      put pArray[k] & cr after valueList
> 
> It doesn't.
>> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list