michaelk Posted October 22, 2021 Share Posted October 22, 2021 I'm trying to sort a dynamic array of handles (to door objects in the drawing) by a field in the parametric record (IDLabel). Is it SortArray(arrayName, total number of items in array(?????), index number of record field (????????); Quote Link to comment
JBenghiat Posted October 22, 2021 Share Posted October 22, 2021 The description explains the sorting: Quote Sorts a 1-dimension array into ascending order. If the array contains handles to records, the array can be sorted by the specified field number index. If the array is an array of structures, the fieldnumber argument denotes the element in the structure on which to sort. The first value is the length of the array. If this is a dynamic array, then you should be tracking the size of the array anyway. The second is the index of your data structure, if you have one. If the array contains handles to record instances, pass the field index that you want to sort by. I can't recall if this also works on PIO's. While vs allows you to access data via the PIO handle, it's technically not a record. If it does work, you can use the Plug-in manager or DebugListView to find the field index. I believe the index is 1-based/ Alternatively, you can sort an array of structures (see the vs language guide). You then pass on which element of the struct you want to sort, based on the order of fields in your structure. This is probably a better approach, as you don't have to rely on the index of the field — you can use GetRField to retrieve the data and store it into your structure. You can also concatenate data in order to create a primary and a secondary sort. Sorting data in vs is definitely clunky, and with Python as an alternative, not likely to ever improve. 1 Quote Link to comment
MullinRJ Posted October 22, 2021 Share Posted October 22, 2021 @michaelk, Your array of handles needs to be an array of handles to the DOOR RECORDs, and not the DOOR INSTANCE handles. With the handles to records you can access the field with the third argument of SortArray(). Plus, everything Josh said. Raymond Quote Link to comment
michaelk Posted October 22, 2021 Author Share Posted October 22, 2021 Aha. That makes more sense :-). Thanks, both of you. I read that section in the function reference over and over and never understood it. Quote Link to comment
michaelk Posted October 23, 2021 Author Share Posted October 23, 2021 I should really read my own code. I'm trying to sort a dynamic array of strings. This seems to work - no matter what number I put in the 3rd argument: PROCEDURE Test; CONST CR = CHR(13); VAR StArray : DYNARRAY[] of STRING; Sorted, UnSorted,SortTest : STRING; BEGIN ALLOCATE StArray[1..6]; StArray[1] := 'Foxtrot'; StArray[2] := 'Echo'; StArray[3] := 'Delta'; StArray[4] := 'Charlie'; StArray[5] := 'Bravo'; StArray[6] := 'Alpha'; UnSorted := Concat( StArray[1],CR, StArray[2],CR, StArray[3],CR, StArray[4],CR, StArray[5],CR, StArray[6],CR); SortArray(StArray,6,1); Sorted := Concat( StArray[1],CR, StArray[2],CR, StArray[3],CR, StArray[4],CR, StArray[5],CR, StArray[6],CR); SortTest := Concat( 'Unsorted: ',CR, Unsorted, CR, CR, CR, 'Sorted: ',CR, Sorted); BeginText; SortTest EndText; END; RUN(Test); Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.