Another (variable length) Array Sort for integer numbers

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
Dick99999
Pro Scripter
Posts: 84
Joined: Thu Nov 27, 2008 10:25 am
Location: Netherlands

Another (variable length) Array Sort for integer numbers

Post by Dick99999 » Fri Aug 13, 2010 7:56 am

The following code sorts an array that contains integer numbers. Of course an extended MS ArraySort with a choice of alfa/numeric would be preferred, but as long as it's not there this seems to work for small arrays.

This sort also copes with the length issue of what would be variable length arrays in MS. The second parameter of the subroutine specifies the length: a caller set length independent of the real (but larger) length of the array. Or set it to a -1 length means the sort has to use the implicit 'count length' of the array.

=======Edited
Changed the script with respect to local vars. The sort script now restores the state of localvars to that when called

Code: Select all

SRT>arraySortNum

// calling format
//                 gosub>arraySortNum,myArray,-1       if length of array should be used
//         or      gosub>arraySortNum,myArray,myLen    if length is specified, and lenth of array is not used. Part of array above myLen is not changed!
//         and     set all 9000 numbers to (largest number expected * 10) and upscale that number to let it start with a nine
//                 so if largest number is 800, then upscale 10*800 is 8000 to 9000

    let>arraySortNum_VAR_lVar=LOCALVARS
    let>LOCALVARS=0
    if>arraySortNum_VAR_2<0
          //                                           set _n to [n] if that is the array format used, and 'exchange the 2 array references and the comments below
          ArrayCount>arraySortNum_VAR_1,arraySortNum_VAR_L,_n
       else>
          let>arraySortNum_VAR_L=arraySortNum_VAR_2
    Endif>
    
    let>LOCALVARS=1
    
    let>n=0
    while>n<arraySortNum_VAR_L
        let>n=n+1
//            set all 9000 numbers to (largest number expected * 10) and upscale that number to let it start with a nine
        let>localArray_%n%=%arraySortNum_VAR_1%_%n%+9000
//      let>localArray_%n%=%arraySortNum_VAR_1[%n%]+9000
    endwhile
    arraySortNum>localArray

    let>nn=0
    while>nn<arraySortNum_VAR_L
        let>nn=nn+1
        let>LOCALVARS=0
        let>%arraySortNum_VAR_1%_%nn%=localArray_%nn%-9000
//      let>%arraySortNum_VAR_1[%nn%]=localArray_%nn%-9000
        let>LOCALVARS=1
    endwhile>
    let>LOCALVARS=arraySortNum_VAR_lVar
END>arraySortNum
and this is the test, if used with the debugger one can follow the results. Note that arrays start at 1 not zero

Code: Select all


// just code to test it, can be deleted
       let>myArray_1=300
       let>myArray_2=23
       let>myArray_3=222
       let>myArray_4=2
       let>myArray_5=10
       let>myArray_6=1
       let>myArray_7=-300
       let>myArray_8=-222
       let>myArray_9=-2
       let>myArray_10=-1
       let>myArray_11=-10
       let>myArray_12=999
       let>myArray_13=-999

       **BREAKPOINT**
       gosub>arraySortNum,myArray,10
       **BREAKPOINT**
       gosub>arraySortNum,myArray,-1
       **BREAKPOINT**

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts