XML: Bubble Sort utilizing XMLVARs

From FSDeveloper Wiki
Revision as of 10:39, 17 February 2014 by Rpmc (talk | contribs) (Created page with " Recently, a question about sorting has come up. The context was an ICAO Search of Facility types which returns ICAO strings in alphabetical order. However, each ICAO rep...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


   Recently, a question about sorting has come up. The context was an ICAO Search of Facility types which returns ICAO strings in alphabetical order. However, each ICAO represents a Facility that has a certain distance from the user's aircraft, and a sort according to distance is needed.
   Acknowledgements are owed to Robbie McElrath who hammered out the Bubble Sort XML in a few minutes (thank you, Robbie) and Tom Aguilo, for providing XMLVARs. The sort script that follows makes use of the array-forming capabilities of XMLVARS. Thanks, Tom.
   Code:
    <Macro Name="sort">
      @2 (>L:sort_len, number)
      :127
         0 (>L:sort_swapped, bool)
         0 sp0
         :128
            l0 ++ s0 (L:sort_len, number) >= if{ g129 }
            @1 l0 -- scat s10 (>C:XMLVARS:SearchVarName, string) (C:XMLVARS:NumberValue, number) s11
            @1 l0    scat     (>C:XMLVARS:SearchVarName, string) (C:XMLVARS:NumberValue, number) s12 >
                if{
                  l11 (>C:XMLVARS:NumberValue, number)
                  l10 (>C:XMLVARS:SearchVarName, string) l12 (>C:XMLVARS:NumberValue, number)
                  1 (>L:sort_swapped, bool)
                }
            g128
         :129
         (L:sort_swapped, bool) if{ g127 }
    </Macro>

    <Macro Name="store">
      @1 @2 scat (>C:XMLVARS:StoreVarName, string) (>C:XMLVARS:NumberValue, number)
    </Macro>  

    <Update Frequency="18" Hidden="No">      
    (L:SortInit, bool) 0 ==
        if{
            2 @store('sort_test', 0)
            1 @store('sort_test', 1)
            4 @store('sort_test', 2)
            8 @store('sort_test', 3)
            7 @store('sort_test', 4)
            0 @store('sort_test', 5)
            1 (>L:SortInit, bool)
        }  
                   
        'sort_test0' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest0, number)       
        'sort_test1' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest1, number)  
        'sort_test2' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest2, number)  
        'sort_test3' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest3, number)  
        'sort_test4' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest4, number)  
        'sort_test5' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest5, number)  
    </Update>

    <Area Left="150" Top="130" Width="16" Height="16">
        <Cursor Type="Hand" />
            <Click Kind="LeftSingle+RightSingle">
                (L:SortMacroExecuted,enum) ++ (>L:SortMacroExecuted,enum)
                @sort('sort_test', 6)
            </Click>
    </Area>    
   Notes:
       (L:sort_len, number) is the array length. For example, IcaoSearchMatchedIcaosNumber
       The SortInit sequence was used for testing purposes only. It stores 2, 1, 4, 8, 7, and 0 into XMLVARs named 'sort_test0' through 'sort_test5'. In a real application, GeoCalcDistance information would actually generate the values to be stored in these XMLVARs and a SortInit sequence would probably never be needed.
       The bottom 6 lines in the Update section simply read the XMLVARs and store then into traditional L:Vars so that BlackBox can display them (so you can see what is happening before and after the sort is applied)
       A Mouse Area 'click area' is used to execute the sort macro, @sort('sort_test', 6). The number 6 is the array length to be sorted. In the real example I postulated, it would be written @sort('sort_test',C:fs9gps:IcaoSearchMatchedIcaosNumber)
   The following is a screen shot of a BlackBox display. On top are the initial values of (L:SortTest0, number) through (L:SortTest5, number). On the bottom are the values after the bubble sort. Tested in FS9 but of course, also works in FSX (XMLVARs has a FS9 module and a FSX module)