Data AcQuisition And Real-Time AnalysisScope - Spectrum - Spectrogram - Signal Generator
Software for Windows
Science with your Sound Card!
Contact us about
Macro Array Sort Operations
Simple macro commands can sort the elements of a Macro Array (Buf0-Buf7), or any portion of the array, into ascending or descending order.
You can link other arrays to a previously-sorted array, so their elements will be arranged in the same sequence as the sorted array, independent of their actual values.
Linked sorts are especially useful for shuffling arrays into random order. See the Parallel Array Shuffle subtopic under Random Macro Values.
You can also create a look-up table to find the original position (index) of any value given its sorted index, or a table to find the sorted position given the original index.
Besides numerical sorts, these same operations can be applied to arrays of short alphanumeric items (4 characters or less each).
Buf0="<s(A,B)" Sort Buf0 over index range A through B Buf0="<sa(A,B)" Alphanumeric sort of 8-character strings Buf1="<sX(A,B)" Transfer (link) prior sort to Buf1 Buf2="<sO" Fill Buf2 with Original indexes Buf3="<sN" Fill Buf3 with New indexes
Buf0="<s(0,255)" sorts the Buf0 array from element Buf0 through Buf0 into ascending numerical order. Other elements in this array are untouched.
Buf0="<s(255,0)" sorts the same range into descending order.
You can use variables or expressions instead of immediate values for the sort range, as in Buf0="<s(UA,UB)".
If you have stored 8-character (max) strings into Buf0 using Buf0#a="Melon", Buf0#a="Cabbage", Buf0#a="Peach", etc, you can sort them using Buf0="<sa(UA,UB)" where UA and UB are the first and last indexes.
As with the above basic numerical sort, the index order determines the sort order: If the lowest index is given first, the entries will be sorted into ascending order; if the highest index is first, they will be sorted into descending order.
Note that this only supports string arrays using the #a parameter to hold strings up to 8 characters in each element; it does not sort multi-element strings of 16, 32, or more characters using #a2, #a4, or beyond.
Important: If you use only 4-character strings, entered via Buf0="Text", etc, (without the #a parameter), then you should use the basic Buf0="<s(UA,UB)" numerical sort instead of the <sa alphanumeric version. That's because #a strings are stored with the characters in reverse order, so the <sa version reverses the order for the sort, then reverses them back.
The sort operation builds an internal map that can be transferred to other arrays. This is analogous to the way a database or spreadsheet program allows you to sort on one column, but keep linked columns in that same sorted order.
For example, Buf0 may hold part numbers, while Buf1 holds distortion measurements for each test on 100 parts. Buf1="<s(0,99)" will sort from lowest to highest distortion. To transfer (Xfer) that sort order to the part number array, use:
Buf0="<sX(0,99)" ;Part numbers, low to high distorion
Now you can read the 0th element of Buf1 to find the lowest distortion, and the same element of Buf0 to find which part had that distortion.
Note that the 'sX' sort transfer command does not specify which previously-sorted array it is linked to; the transfer always uses the order of the most recent basic (non-transfer) sort. You can then apply that same sort order to as many other arrays as needed.
This includes alphanumeric string storage saved with the Buf0#a="AlphaNum" format (up to 8 characters each), but not longer strings using the #a2, #a4, or larger formats.
After an initial numerical or alphanumeric sort, you can create two different types of look-up tables.
Buf2="<sN" fills Buf2 with the New index values relative to their original positions. If you want to know where the Nth element in the original array went after the sort, UN=Buf2[N] will set UN to the new index.
Buf3="<sO" does the opposite: It fills Buf3 with the Original (or Old) index values. If you want to know where the Nth element in the sorted results was before the sort, then after this command you can use UO=Buf3[N] to set UO to the original index.
Below is a table illustrating a sort of the first 12 values in a buffer which was initially filled with random values between 0 and 100, as shown in the Raw column.
Consider, for example, the value at index 4, which started out holding 21.976. After the sort that value moved to index 2, which you can see by following the index 4 row across to the New column. Conversely, if you want to know where the Sorted index 4 value of 49.631 was originally, follow across to the Old column and you will see that it was at index 6.
Index Raw Sorted New Old 0 15.683 9.066 1 3 1 56.889 15.683 6 0 2 56.557 21.976 5 4 3 9.066 32.716 0 10 4 21.976 49.631 2 6 5 83.975 56.557 10 2 6 49.631 56.889 4 1 7 99.514 69.803 11 9 8 79.217 77.882 9 11 9 69.803 79.217 7 8 10 32.716 83.975 3 5 11 77.882 99.514 8 7
Questions? Comments? Contact us!We respond to ALL inquiries, typically within 24 hrs.
Over 35 Years of Innovative Instrumentation
© Copyright 2007 - 2021 by Interstellar Research
All rights reserved