sponsor Vim development Vim logo Vim Book Ad

variableSort.vim : Function to sort a simulated array variable set.

 script karma  Rating 0/0, Downloaded by 925  Comments, bugs, improvements  Vim wiki

created by
Salman Halim
script type
This is  a sorting function which  takes the name of  an array-like variable
which ends  in numerical  subscripts along  the lines  of var0,  var1, var2.
These can be created quite easily with the var{i} notation of Vim 6.

Other parameters are the starting  and ending subscripts (inclusive) and the
direction of the sort (1 or 'a' for ascending, otherwise descending).

The array is sorted in place; there is no return value from the function.

Optional  argument  is the  name  of  a swap  function  that  takes the  two
subscripts  and  swaps the  variable  (there  is  one provided  that  should
actually be good enough for most  cases; however, if something special needs
to be  done for the swap  -- such as  multiple records, then this  should be
used.  In this  case, it  is  assumed the  swap function  already knows  the
variables it's going to swap and thus aren't passed in.)

Sample custom swap function:
function! MultipleRangeSwap( sub1, sub2 )
  let temp = b:rangeS{a:sub1}
  let b:rangeS{a:sub1} = b:rangeS{a:sub2}
  let b:rangeS{a:sub2} = temp

  let temp = b:rangeE{a:sub1}
  let b:rangeE{a:sub1} = b:rangeE{a:sub2}
  let b:rangeE{a:sub2} = temp

Sample call:
call SortArray( "b:rangeS", 0, b:numRanges - 1, 1, "MultipleRangeSwap" )

This will perform an ascending sort  on a variable sequence named b:rangeS0,
b:rangeS1 etc.,  using the MultipleRangeSwap  function for the  actual swap.
This function  was needed here  because two  variables needed to  be swapped
together  even though  only  one was  used  in the  sort.  The default  swap
would've only swapped b:rangeS variables, leaving the b:rangeE variables out
of sync.
install details
Pop into your plugin directory.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
variableSort.vim 1.0 2002-07-26 6.0 Salman Halim Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Vim at Github