Hello Everybody,
OCL has a number of various collection types with related operations which is usually quite handy and allows an easy notation of complex collections based on other collections. Moreover, it is also possible to convert from one collection type to another one using operations like asSet, asSequence , asBag or asOrderedSet. Especially the last one (resp. the collection type OrderedSet and the conversion to it) do need some special care: The word Ordered does here not mean any ordering of the elements of that collection by their value or by values of their attributes (e.g. numerical or alphabetical order), but rather that each element of the OrderedSet has a fixed position in the set. The rules, which element of the OrderedSet get's which position is depending on the source from which the OrderedSet is created (OCL Definition from OMG):

See also some examples below - which indicate that in most you can not rely on any specific order (and not expect thta the first or last entry is the largest/smallest):
Evaluating:
Set{3, 1,3,5, 2.0, '3'}->asOrderedSet()
Results:
2.0
1
3
'3'
5
Evaluating:
Sequence{3, 1,3,5, 2.0, '3'}->asOrderedSet()
Results:
3
1
5
2.0
'3'
Evaluating:
OrderedSet{3, 1,3,5, 2.0, '3'}->asOrderedSet()
Results:
3
1
5
2.0
'3'
On the other side you can apply for an OrderedSet position based operations similar to a Sequence (like append, prepend, first, last, at ...) - which of course do maintain the characteristics of an OrderedSet being a set (i.e. an element may occur at most once).
If you need for a collection a specific order, OCL provides you with an additional operation sortedBy(OCLExpression) for all kinds of collections. This will return in general a sequence of the elements of the original collection sorted by the OCLExpression. In case the original collection was an OrderedSet it will return again an OrderedSet but now also sorted by the OCLExpression. Note: The OCL expression is applied to all elements of the collections, so they (resp. the values addressed by the expression) need to have the same type and of course this type needs to support the '<' operator. Here is one example - where self is an FMEA Failure Cause and we want to have the measure groups sorted by their occurrence - here we use the attribute occurrence for the sorting:
self.currentDesignControls.element.oclAsType(safetyModel::MeasureGroup)->sortedBy(occurrence).occurrence
And in case the original collection is just a collection of values of a basic type (e.g. number or string):
Set{'ASIL D', 'QM','ASIL A', 'ASIL C','QM'}->asOrderedSet()->sortedBy(value|value)
Results:
'ASIL A'
'ASIL C'
'ASIL D'
'QM'
Happy Scripting
Eckhardt