- Messages
- 1,564
- Country

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 user's aircraft rather than alphabetical order of the ICAO strings.
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.
Notes:
The following is a screen shot of results. 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 also works in FSX (XMLVARs has a FS9 module and a FSX module)
Bob
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.
Code:
<!-- @sort('array_name', array_length) -->
<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: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>
<Area Left="150" Top="130" Width="16" Height="16">
<Cursor Type="Hand" />
<Click Kind="LeftSingle+RightSingle">
@sort('sort_test', 6)
(L:SortMacroExecuted,enum) ++ (>L:SortMacroExecuted,enum)
</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). (L:SortMacroExecuted,enum) ++ (>L:SortMacroExecuted,enum) was included for test purposes to verify operation of the mouse click. Obviously, it isn't necessary.
- 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 sort in descending order, change the > to < in this line:
The following is a screen shot of results. 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 also works in FSX (XMLVARs has a FS9 module and a FSX module)
Bob
Last edited:

Now to learn this.








