Using Sort Options
The following examples demonstrate how you can use the sort options to control sorting behavior.
The specific examples use the sort statement, but the sort options can also be used with lists. For more information, see Sorting Lists and Sublists.
Sort Order
You can explicitly set the search order by specifying the ascending or descending search option (or their abbreviations). For example, given a set of occurrences of entity TEL_CODES:
Use the following command to sort in descending order by country:
sort/e TELCODES, COUNTRY: descending
By default, Uniface sorts data using a binary sort order. However, if the locale is specified, it can apply locale-based rules for sorting data using $nlssortorder. For more information, see Sorting Based on Locale.
Removing Items with Duplicate Values
It is possible to remove duplicate items from a
list using the unique
sort option. For example, an entity TELCODES has fields
representing the international calling codes for countries (ICODE and COUNTRY). Both Canada and the
United States have the international calling code of 1
:
The following ProcScript instruction sorts the list by calling code and removes occurrences with a duplicate value:
sort "TELCODES.TEL", "ICODE: u"
Only the first occurrence of the duplicate value is included in the result.
Sorting Using Data Type
When sorting occurrences, Uniface uses the data type of the specified sort field. Thus, a numeric field is sorted in numerical order, a Date field is sorted according to its internal data format, and will therefore sort correctly. ( For more information, see Internal Data Format.) If you specify a data type or data type format, for example Numeric or $number, it overrides the field's default data type.
When sorting lists, all data is treated as string data by default.
Specifying another data type is particularly useful when the ElementToSortBy has data type String but contains numbers, because the sort is by character, which results in 100 sorting before 19. For example, the TELCODES entity has its field PK defined as a string. The following ProcScript instruction sorts occurrences by the PK field:
sort/e "TELCODE", "PK: a"
This results in PK=11
sorting
before PK=2
.
To sort the field as a numeric data type, use the following ProcScript instruction:
sort/e "TELCODE", "PK: a numeric"
Sorting by Hierarchical Level
You can also specify a hierarchical sort if the ElementToSortBy contains numbers that represent a hierarchy, such as n.n.n, where each period represents another level in the hierarchy. For example, the following list of telephone are codes for Canada has a LEVEL field which is used to group the area codes by province.
To sort using the normal string sort order, use the following ProcScript:
sort/e AREACODES
In this case, 1.10
sorts before
1.2
:
To sort on the basis of the hierarchical level, use the following ProcScript instruction:
sort/e AREACODES, "LEVEL: level"
Now 1.2
sorts before
1.10
Sorting by Multiple Elements
Uniface enables you to sort by more than one element or field, which is also known as multi-level sorting. For example, a single area code may be assigned to multiple cities or regions.
To sort by AREACODE and then by REGION:
sort/e "AREACODES.TEL", "AREACODE: a ;REGION: a"