Jump to content
Developer Wiki and Function Reference Links ×

Sorting Arrays


Assembly

Recommended Posts

I have a Menu Function that can align selected object. It makes a column of items.

The code below aligns selected custom PIO -'NZ MSpec Legend '.

I am using Foreachobject to use the GetInfo Procedure to load an array with the handles of the selected object and two records.

This procedure will align the objects in the order they are created in the layer. I would like to be able to use the CBI string (or number) to order the array. I would then like to sort Alphabetically the Keynotes.

I don't have a lot of programming experience, but guessing that sorting arrays might be a common thing to do. Is it? If so can anyone give a little guidance.

Thanks.

PROCEDURE AlignObj;

TYPE

Level1=Structure

hObject:Handle;

X1,Y1,x2,y2:Real;

CBI:STRING;

KeyNote:STRING;

END;

VAR

Obj: Array[1..500] OF LEVEL1;

i,icount:INTEGER;

p:INTEGER;

X1,Y1,x2,y2:Real;

FUNCTION GetInfo(h:handle):Boolean;

BEGIN;

IF ((h <> NIL) & (GetType(h) = 86) & (Eval(h,(R IN ['NZ MSpec Legend ']))>0)) THEN

BEGIN

Obj.hObject:=h;

GetBBox(h,Obj.x1,Obj.y1,Obj.x2,Obj.y2);

i:=i+1;

Obj.CBI:=GetRField(h, 'NZ MSpec Legend ', 'CBI');

Obj.KeyNote:=GetRField(h, 'NZ MSpec Legend ', 'KeyNote');

END;

END;

BEGIN;

i:=1;

ForEachObjectInList(GetInfo,2,0,FSObject(ActLayer));

iCount:=i;

For i:= 2 to iCount DO

BEGIN;

IF Obj.hObject<> NIL

THEN

BEGIN

hMove(Obj.hobject,-Obj.x1,-Obj.y1);

GetBBox(Obj[i-1].hobject,x1,y1,x2,y2);

hMove(Obj.hobject,x1,y2);

END;

END;

END;

RUN (AlignObj);

Link to comment

Since you already have them in an array, you are in luck.

VS has a function called:

PROCEDURE SortArray

( VAR arraytosort :ARRAY;

numtosort :INTEGER;

fieldnumber :INTEGER

) ;

numtosort is how much of the array you want to sort. Typically this will be the entire array and either the number you defined it as or the current max if it is a variable array.

fieldnumber is the column you want to sort on.

This is all in the VS Function Reference.

Link to comment

Or you could do it manually, here's an example to do so:

FOR cnt1 TO total-1 DO
BEGIN
FOR cnt2 TO total-1 DO
BEGIN
	IF array[cnt1] > array[cnt1+1] THEN
	BEGIN
		tmp:=array[cnt1];
		array[cnt1]:=array[cnt1+1];
		array[cnt1+1]:= tmp;
	END;
END;
END;

{and here's the same code, bug with some more information (not sure if it makes it more clear or even the opposite  )}
{repeat this part for as many index-1 the Array has}
FOR cnt1 TO total-1 DO
BEGIN
{run trough the Array except for the last index}
FOR cnt2 TO total-1 DO
BEGIN
	{if the index you're checking now is bigger then its follower...}
	IF array[cnt1] > array[cnt1+1] THEN
	BEGIN
		{... switch the values of those two}
		tmp:=array[cnt1];
		array[cnt1]:=array[cnt1+1];
		array[cnt1+1]:= tmp;
	END;
END;
END;

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