Jump to content
Developer Wiki and Function Reference Links ×

sorting arrays of objecthandles


Hippocode

Recommended Posts

Situation:

I load X objecthandles into an array. These objects all have a (not unique) name in a parameterfield of that object.

When using the array, I'd like to have the array to be sorted on those names before using it any further.

Since there is no function to sort a multidimensional array, does anyone have a workaround scripted ?

Link to comment
Instead of using a multidimensional array, use an array of custom structures. (See the VS Language Guide if Structures are a new concept).

Then you can use SortArray http://developer.vectorworks.net/index.php?title=VS:SortArray, passing the number corresponding to the order that you declared the "name" variable in your custom TYPE definition as the last parameter.

-Josh

I tried that, but sortarray; doesn't work on a handle of an object so euhm how does one do that ?

Link to comment

I believe it does, it start on page 29 of the Language Guide.

And the VS Func Ref states: "If the array is an array of structures, the fieldnumber argument denotes the element in the structure on which to sort."

So, nothing wrong with the documentation I think (or this part at least, the rest could use some extra info ;) )

Structures are very powerful and extremely handy!

Link to comment
  • 1 month later...

The function can only sort one column but if you add another column/field in the array/structure and place the sorting criteria there such as Concat(sortfield1,sortfield2), it will have the same effect as multiple sorting.

The only implication is when the criteria has numbers and characters. The numbers will have to be converted to a string and prefixed/padded with spaces or zeros to sort in numerical order (i.e. 001,002,003,..,010,..,100). This method can also be used in worksheets as '1','10','100' are sorted before '2'.

Link to comment
The function can only sort one column but if you add another column/field in the array/structure and place the sorting criteria there such as Concat(sortfield1,sortfield2), it will have the same effect as multiple sorting.

I can't really follow in adding that to the function

SortArray(StructureName,handleArrayAlloc,2); 

It needs an integer to sort on a field, I don't see how I can sort on field 2 and 3 combined.

Edited by hippothamus
Link to comment

It needs an integer to sort on a field, I don't see how I can sort on field 2 and 3 combined.

Let's say you have 2 fields to sort:

1. Create another field in the array/structure for sorting.

2. Combine the fields to be sorted with Concat(field1,field2)

3. Assign this value to the added sort field.

4. Use SortArray and sort on the added field sequence number, which is the integer needed in the function. I usually set this sorting field as the first one for easier debugging.

It might also be easier to use an array of a structure rather than a multiple column array because this last one can only be one type and you typically want to capture more than one type in one row of the array such as numbers,strings, and booleans.

Link to comment
  • 1 month later...

It needs an integer to sort on a field, I don't see how I can sort on field 2 and 3 combined.

Let's say you have 2 fields to sort:

1. Create another field in the array/structure for sorting.

2. Combine the fields to be sorted with Concat(field1,field2)

3. Assign this value to the added sort field.

4. Use SortArray and sort on the added field sequence number, which is the integer needed in the function. I usually set this sorting field as the first one for easier debugging.

It might also be easier to use an array of a structure rather than a multiple column array because this last one can only be one type and you typically want to capture more than one type in one row of the array such as numbers,strings, and booleans.

I see what you did there.

I'm now using an array of structure and managed to sort on 2 different integers, combining them into a sortfield. Str2Num(concat(field1,',',field2));

What about sorting on multiple fields being strings AND integers ? I guess I need to force zeros infront of all the numbers ugh

Edited by hippothamus
Link to comment
  • 2 months later...

Please help,

I have no luck with Str2Num function

What can be wrong in this procedure, while say TextObject=40.25' is a string, I get correct HeightString= 41.25', but for variable locus

I get zlocus=0 no matter what is HeightString.

VAR

HeightString:STRING;

xlocus,ylocus,zlocus,xtext,ytext:REAL;

TextObject:HANDLE;

HeightString:=GetText(TextObject);

zlocus:=Str2Num(HeightString);

Message('Locus 3D z= HeightString ', HeightString, 'zlocus= ', zlocus);Wait(4);

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