Sorted Associations
Associations
noreferences

@@description
<p>      
It is possible to arrange for associations to maintain themselves in sorted order
according to the value of an attribute. Create a <a href="DerivedAttributes.html">derived attribute</a> in order to define a complex sort order or to sort on the value of an attribute of an associated class (the first example below does this in one case).
</p>

<p>To declare an association as sorted do the following: after specifying the multiplicity, and any role name, give the keyword 'sorted' followed by the name of an attribute (sort key) in curly brackets. Whenever an add method is called to add an item to the association, Umple ensures the association is maintained in sorted order. .</p>

<p>
Note that Umple associations are always 'ordered' by default; i.e. the user controls the position of items. Declaring an association as sorted makes this automatic. The presence of 'sorted' adds a 'sort' function to the API to allow for resorting (if the attributes on which sorting is based are changed, for example) and removes the API for manually ordering the association using addXAt().
</p>

<p>Errors are generated if the attribute specified as the <a href="E025SortKeyNotFound.html">sort key attribute does not exist in the class</a>, or the <a href="E024SortKeyNotofUmpleType.html">sort key attribute is not of a recognized type</a>.</p>

<p>Java serialization of sorted associations is possible without the need to modify the Java code generated by Umple. The two ends of a sorted association implement the Serializable interface by default. Association items can be serialized and deserialized. After deserialization, more items can be added to the association and the association is maintained in sorted order as demonstrated by the second example. </p>

@@syntax
[[inlineAssociationEnd]] [[isSorted]]

@@example
@@source manualexamples/SortedAssociations1.ump
@@endexample

@@example @@caption Example of sorted associations serialisation in Java  @@endcaption
@@source manualexamples/SortedAssociations2.ump
@@endexample
