Because SmartAssembly arrays can contain more variety of information that just alpha / numeric values, we are not able to apply a command to sort the array. When we need to sort the information in an array, we apply simple logic to sort the data either as it goes into the array, or we post-sort the data into a different array.
With sorting, there are a lot of options out there to reference. I think the sorting method you choose depends on the type of data you're sorting and the amount of data you're sorting. You will find that each sorting method has advantages and disadvantages depending on the actual scenario.
I believe the sorting approach you implemented is called the "bubble sort" method which works fine for small data sets but is inefficient for large data sets. Another simple sorting method to try is the "Insertion sort" method. It is likely more efficient than the bubble sort method as it probably has less loops overall. If you find performance to be a big issue, then you'd have to look into more advanced sorting methods. (you'd be surprised how much research goes into finding optimum sorting algorithms. Some colleges have entire courses dedicated to this topic.)
As the name implies, the bubble method enables items to bubble up to the top as it loops so it might actually take several loops to move an item to it's proper location. http://en.wikipedia.org/wiki/Insertion_sort
I don't want to derail this topic, but this looked like a good place to find an answer.
Can you add two arrays together?
I have two arrays. Array_all_prts with a size of 6 andArray_all_asms with a size of 3. I tried adding each array into a third array Array_all_components via Add_array_elem and SA returns a size of 2 forArray_all_components - which i believe would be the two array. Basically I want to confirm that the total number of elements is 6+3 = 9.
Thanks in advance,
When you say you want to add two arrays together, you need to clarify a bit. Confirming the total number of elements should be easy enough using the GET_ARRAY_SIZE command. You can also load elements into the same array, like this:
SEARCH_MDL_REFS THIS ASSEMBLY * myArray
SEARCH_MDL_REFS THIS PART * myArray
This will load all assemblies and parts in the current model into the same array with the assemblies first. Now, you can grab both assemblies and parts at the same time using the COMPONENT reference like this:
SEARCH_MDL_REFS THIS COMPONENT * myArray
Grabbing the references this way will store them in the exact order that they are in the model tree. One of my most common mistakes is forgetting to clear an array first if I don't want to append to it.
What you explained in your post is that you created an array and then you added the two arrays into the array, which means you have an array of arrays which explains why you have an array size of 2. Does this help?
That was simple! I was trying to read a directory and didn't want to read files that weren't a .prt or .asm. I didn't realizeelements would load into the same array - I thought they would be overwritten. Now I know.
Scot, you are very welcome. If you think arrays are fun, just wait til you get into maps.... 🙂
If you had two previously filled arrays, you can also use a FOR loop to get each element from the one array, then use the ADD_ARRAY_ELEM command to put those items into the other array.