Jump to content
Developer Wiki and Function Reference Links ×

Sort Array


michaelk

Recommended Posts

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.

  • Like 1
Link to comment

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);

 

Link to comment

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...