Hippocode Posted January 24, 2013 Share Posted January 24, 2013 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 ? Quote Link to comment
JBenghiat Posted January 24, 2013 Share Posted January 24, 2013 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 Quote Link to comment
Hippocode Posted January 24, 2013 Author Share Posted January 24, 2013 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 ? Quote Link to comment
JBenghiat Posted January 24, 2013 Share Posted January 24, 2013 Your custom structure looks something like this: TYPE OBJECTDATA = STRUCTURE han :HANDLE; name :STRING; END; VAR objectList :ARRAY[1..50] OF OBJECTDATA; BEGIN objectList[1].han:= ..... objectList[1].name:= ..... ..... SortArray(objectList, 2); -Josh Quote Link to comment
Hippocode Posted January 24, 2013 Author Share Posted January 24, 2013 WOW that's awesome, had no idea this kind of functionality is available. Why isn't this in the vsguide ! Thanks it worked out now Quote Link to comment
maarten. Posted January 24, 2013 Share Posted January 24, 2013 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! Quote Link to comment
Hippocode Posted January 25, 2013 Author Share Posted January 25, 2013 Silly me, I've always been looking at the vsguide instead of the language guide. Quote Link to comment
Hippocode Posted March 1, 2013 Author Share Posted March 1, 2013 Is it possible to sort on multiple structure fields ? The same way you can sort in order in worksheets. Quote Link to comment
Miguel Barrera Posted March 1, 2013 Share Posted March 1, 2013 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'. Quote Link to comment
Hippocode Posted March 4, 2013 Author Share Posted March 4, 2013 (edited) 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 March 4, 2013 by hippothamus Quote Link to comment
Miguel Barrera Posted March 4, 2013 Share Posted March 4, 2013 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. Quote Link to comment
Hippocode Posted April 5, 2013 Author Share Posted April 5, 2013 (edited) 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 April 5, 2013 by hippothamus Quote Link to comment
Pat Stanford Posted April 5, 2013 Share Posted April 5, 2013 Just do multiple sorts. Start with the least important (most sub criteria) and work out to the most important. So sort on Street Number, then Street, then City, then State to get a list of addresses sorted by state. Quote Link to comment
ashot Posted July 1, 2013 Share Posted July 1, 2013 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); Quote Link to comment
Pat Stanford Posted July 1, 2013 Share Posted July 1, 2013 I don't think Str2Num handles strings with unit marks. It will only handle pure numeric strings. If the data is consistent enough you could use the string manipulation functions to remove the unit marks. Check the reference at developer.vectorworks.net for Str2Num for more information. Quote Link to comment
ashot Posted July 2, 2013 Share Posted July 2, 2013 Yes, Patrick, you are right, the foot mark at the end of number is causing a problem. With Delete() procedure I cured the problem. Quote Link to comment
JBenghiat Posted July 2, 2013 Share Posted July 2, 2013 Ash, Look at ValidNumStr in the Utillities category. It handles unit marks as well as things like trailing spaces. Developer.vectorworks.net even has a model for rewriting Num2Str. -Josh 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.