Do Collections preserve ordering?

jeff_shattock
Participant

Do Collections preserve ordering?

jeff_shattock
Participant
Participant

Will a collection always keep the same order that items were added to it?  For example, if I have a sketch with three lines in it, will the SketchLines collection that holds them ALWAYS have the first line added as element 0, the second as 1, and the last as 2? 

1 Like
Reply
395 Views
3 Replies
Replies (3)

goyals
Autodesk
Autodesk

Though Collection is like an array of objects but I do not think it is right to make assumption that you will get the elements from an API passed through a collection object always in same order.



Shyam Goyal
Sr. Software Dev. Manager
0 Likes

BrianEkins
Mentor
Mentor

Shyam is correct, you should expect the contents of a collection to be in a particular order or even be returned consistently in the same order.  However, there are a few exceptions to this.  Getting the collection of TimelineObjects from the Timeline object will return the objects in the same order as they exist in the timeline.  Getting the Occurrence objects from a component will return them in the same order as they exist in the browser.  There are probably a handful of other examples like that but they're the exception to the rule.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
1 Like

BrianEkins
Mentor
Mentor

It's not letting me edit my previous post, but I meant to say "you should NOT expect the contents of a collection to be in a particular order".

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes