You use orderGeneric in OCL when you need to sort a collection by multiple expressions and choose the sort direction for each expression.
Purpose
orderGeneric sorts a collection using one or more sort expressions. Each sort expression is followed by an OclSortDirection value that specifies whether that expression sorts in ascending or descending order.
Use the first expression as the primary sort key. If two objects have the same value for that expression, orderGeneric uses the next expression and direction as the next sort key.
Syntax
collection->orderGeneric(
expression1, OclSortDirection::ascending|descending,
expression2, OclSortDirection::ascending|descending
// additional expression-and-direction pairs as needed
)
The arguments after the collection must be supplied as expression-and-direction pairs:
| Argument | Meaning |
|---|---|
expression1
|
The primary value to sort by. |
OclSortDirection::ascending
|
Sort the preceding expression from lower to higher values. |
OclSortDirection::descending
|
Sort the preceding expression from higher to lower values. |
expression2
|
The secondary value to sort by when the primary values are equal. |
Example: sort cars by name, then country
Assume that Car has the attributes Name and Country. The following expression sorts cars by Name in ascending order. Cars with the same name are then sorted by Country in descending order.
self.Car->orderGeneric(
Name, OclSortDirection::ascending,
Country, OclSortDirection::descending
)
For example, given these cars:
| Name | Country |
|---|---|
| Atlas | Sweden |
| Atlas | Germany |
| Comet | France |
The result is ordered by Name first. The two Atlas cars are then ordered by Country in descending order, followed by Comet.
When to use orderGeneric
Use orderGeneric when one fixed direction is not enough. For example, use it to show records grouped by an ascending name while placing the highest secondary value first within each name group.
If you only need a descending sort, see Documentation:OCLOperators orderDescending. For other available operators, use the OCL editor or see OCL General Operators.
