XML: Bubble Sort utilizing XMLVARs

From FSDeveloper Wiki
Jump to: navigation, search

Recently, a question about sorting came up. The context was an ICAO Search of Facility types which returns ICAO strings in alphabetical order. However, each ICAO represents a Facility that is a certain distance from the user's aircraft, and proper display of the Facility information required a sort according to distance from the aircraft, not alphabetical order of the ICAO string.

Acknowledgements are owed to Robbie McElrath who provided the Bubble Sort XML (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.

 MACROS:
   <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 & gt;
               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:
   <Update Frequency="18" Hidden="No">      
   (L:StoreInit, 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:StoreInit, 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>
 MOUSE:
   <Area Left="150" Top="130" Width="16" Height="16">
       <Cursor Type="Hand" />
           <Click Kind="LeftSingle+RightSingle">
               @sort('sort_test', 6)
           </Click>
   </Area>    

Notes:

- (L:sort_len, number) is the array length. For example, IcaoSearchMatchedIcaosNumber

- The StoreInit 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, for example, would actually generate the values to be stored in these XMLVARs and a StoreInit sequence would probably never be used.

- The bottom 6 lines in the Update section simply read the XMLVARs and store them into traditional L:Vars so that BlackBox can display them (so you can see what is happening before and after the sort is applied). Again, for testing purposes.

- 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 sort macro requires use of XMLVARs. Specifically, the array-forming capability of XMLVARs in which an array of variables can be named dynamically, such as, sort_test0, sort_test1, sort_test2, ... sort_testn, is needed. Without XMLVARs, it is not straightforward to represent the data in the first place.

- The algorithm sorts in ascending order. To reverse it to sort in descending order, change the & gt; to & lt; in this line:

 @1 l0 scat (>C:XMLVARS:SearchVarName, string) (C:XMLVARS:NumberValue, number) s12 & lt;
 FINALLY - Note that I placed a space after the ampersand & in 4 places above (one in the Code and three in the Notes) so that it would display properly in the wiki.  Please remove that space in your XML

Bob