Jump to content

Sort multi-dimension Dynarray?


Recommended Posts

This may not be possible, or else I'm just missing it.

I am writing some scripts which have some multi-dimensional dynarrays. For instance, the dynarray may hold an X position, Y-position, and rotation for each symbol. Now, I know that you can sort a single-dimension dynarray using the sort command, but it seems odd that you can't sort a multi-dimensional dynrray by choosing which "row" you want to use as the sort.

Am I missing something, or would I have to use two separate arrays and some sort of bubble sort to accomplish this?

Link to comment
Guest Frank Brault

Use SortArray:

If arraytosort is an array of records, it sorts on the fieldnumberth field of the record.

PROCEDURE SortArray(

VAR arraytosort :ARRAY;

numtosort :INTEGER;

fieldnumber :INTEGER);

I think that it would actually be sorted on the column.

hth,

Link to comment

Sam,

???If you create a STRUCTURE to represent the column fields of your 2D array, then you can create a 1D array of the structure which is sortable.

Here's an example. Note, you can have field names the same as your variable names, but if that's confusing, then don't do it.

HTH,

Raymond

*****

PROCEDURE SortStructureExample;

{ Sample code for sorting an array of a STRUCTURE. }

{ Gather X, Y, & Rotation of selected symbols and sort them by their Y value. }

{ Place multiple symbols on a design layer, select them, and run script. }

{ Symbols will highlight one at a time from lowest to highest Y value. }

{ 30 Nov 2013 - Raymond J Mullin }

TYPE

???XYR = STRUCTURE

??????h :Handle;??????{ 1st field }

??????x, y, r :Real;???{ 2nd, 3rd & 4th fields }

???end;???{ XYR }

VAR

???H :Handle;

???I, CntSel :Integer;

???SymPos :DynArray [] of XYR;

BEGIN

???CntSel := NumSelectedObjects;

???Allocate SymPos [1..CntSel];

???I := 0;

???H := FSActLayer;

???while (H <> nil) do begin

??????if (GetTypeN(H) = 15) then begin???{ symbol instance }

?????????I := I + 1;

?????????GetSymLoc(H, SymPos.x, SymPos.y);

?????????SymPos.r := GetSymRot(H);

?????????SymPos.h := H;

?????????H := NextSObj(H);

??????end;???{ if }

???end;???{ while }

???SortArray(SymPos, I, 3);???{ I = # of selected symbols, 3 = Y field }

???{ Highlight each symbol from lowest to highest Y value. }

???for I := 1 to CntSel do begin

??????DSelectAll;

??????SetSelect(SymPos.h);

??????wait(1);

??????RedrawAll;

???end;???{ for }

???SysBeep;

END;

Run(SortStructureExample);

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...