Showing results for 
Search instead for 
Did you mean: 
Showing results for 
Search instead for 
Did you mean: 

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Sorting an array by numeric and/or alpha values in Smart Assembly


Sorting an array by numeric and/or alpha values in Smart Assembly

Is there a way to sort an array by numeric and/or alpha values in Smart Assembly? Ascending and descending?


Doug Brandt
Engineering Systems Analyst 2
Development Engineering

This thread is inactive and closed by the PTC Community Management Team. If you would like to provide a reply and re-open this thread, please notify the moderator and reference the thread. You may also use "Start a topic" button to ask a new question. Please be sure to include what version of the PTC product you are using so another community member knowledgeable about your version may be able to assist.

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.

If anyone would like to know how to sort an array in Smart Assy, ascending or descending, for number or alpha values, here is what I came up with.
If someone has a better way, I would like to hear about it.


DECLARE_ARRAY CollectionArray
ADD_ARRAY_ELEM CollectionArray 7
ADD_ARRAY_ELEM CollectionArray 3
ADD_ARRAY_ELEM CollectionArray 5
ADD_ARRAY_ELEM CollectionArray 6

! works with alpha characters too
! ADD_ARRAY_ELEM CollectionArray "J"
! ADD_ARRAY_ELEM CollectionArray "A"
! ADD_ARRAY_ELEM CollectionArray "F"
! ADD_ARRAY_ELEM CollectionArray "Z"

FOR eachElem REF ARRAY CollectionArray
ADD_ARRAY_ELEM CounterArray Counter

GET_ARRAY_SIZE CollectionArray CollectionArraySize
FOR OutterCounter REF ARRAY CounterArray
FOR InnerCounter REF ARRAY CounterArray
IF InnerCounter == CollectionArraySize - 1
GET_ARRAY_ELEM CollectionArray InnerCounter CurNum
GET_ARRAY_ELEM CollectionArray InnerCounter+1 NextNum
! use < for descending order and > for ascending order
IF CurNum > NextNum
SET_ARRAY_ELEM CollectionArray InnerCounter+1 CurNum
SET_ARRAY_ELEM CollectionArray InnerCounter NextNum

FOR eachElem REF ARRAY CollectionArray
PRINT "%" eachElem

PRINT "-- program complete --"


Doug Brandt
Engineering Systems Analyst 2
Development Engineering

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.

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:



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:


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?

Design well!


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

Design well!

Hi Scot,

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.

Top Tags