Reverse a list

Peter M. Brigham pmbrig at gmail.com
Mon Feb 16 18:02:19 EST 2015


So, Alex's way of doing it is the fastest pure-LC way (I didn't get into using the database methods). I referenced the list and turned the function into a command, saves memory (possibly speed?) on very large lists.

on reverseSort @pList, pDelim
   -- reverse-sorts an arbitrary list
   --    ie, line/item -1 -> line/item 1, line/item -2 -> line/item 2
   -- pList is referenced, so the original list will be changed,
   --    more memory-efficient and slightly faster for large lists
   -- pDelim defaults to cr, can be overridden
   -- thanks to Alex Tweedly of the use-LC list
   --    this was the fastest algorithm of many suggested
   
   if pDelim = empty then put cr into pDelim
   set the linedel to pDelim
   put pList into outList
   put the number of chars in outList into currentLen
   repeat for each line thisLine in pList
      put the number of chars in thisLine into lineLen
      put thisLine & pDelim into char (currentLen-lineLen) to currentLen of outList
      subtract (lineLen+1) from currentLen
   end repeat
   delete char -1 of outList
   put outList into pList
end reverseSort

-- Peter

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





More information about the use-livecode mailing list