Sort a string table

Every once in a while I go to use a function in OptoScript that I just assume will exist… and find it missing. Detour to write the function, then back to whatever I was working on. I think it would be great if we could get a collection of that sort of thing going, so here is my first contribution:

Attached is a quick translation of a string table sort into OptoScript, packaged as a subroutine. I slightly modified a bubble sort algorithm to force empty entries to sort to the end of the list.

Please feel free to post any improvements… and please post any useful subroutines of your own.

Here is the actual code in case for any reason you can’t import the attached subroutine:


// nSortLength is parameter passed to specify length within the table to sort
// stTable is parameter passed specifying string table to sort
// n, newn, i are int32 variables local to subroutine

n = nSortLength;
while (n > 1)
  newn = 0;
  i = 0;
  for i = 0 to (n-2) step 1
    if (((CompareStrings(stTable[i],stTable[i+1]) == 1) and not
       (stTable[i+1] == "")) or (stTable[i] == "")) then
      stTemp = stTable[i];
      stTable[i] = stTable[i+1];
      stTable[i+1] = stTemp;
      newn = i + 1;
    endif
  next
  n = newn;
wend