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:

Unsorted List of Countries

Grid with columnsCountry and International Code, unsorted

Use the following command to sort in descending order by country:

sort/e TELCODES, COUNTRY: descending
Countries Sorted in Descending Order

Grid with columns Country and International Code, sorted by Country in descending
    order.

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:

Duplicate Field Values

Grid with columns Country and International Code, showing Canada and the United States
    with the same international code (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.

Unique Field Values

Grid with columns Country and International Code, showing only Canada and not the
    United States.

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.

String Field With Numbers Sorted as String

Grid with columns PK, Country, and International Code, sorted by PK as a string (1,
    11, 2, 25, etc.)

To sort the field as a numeric data type, use the following ProcScript instruction:

sort/e "TELCODE", "PK: a numeric"
String Field With Numbers Sorted as Numeric

Grid with columns PK, Country, and International Code, sorted by PK as a number (1,2,3
    etc.)

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:

Default Sort for Field LEVEL

Grid with columns Level, City/Region, and Area Code, using the default sort order on
    LEVEL, which puts 1.10 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

Field Sorted by Level

Grid with columns Level, City/Region, and Area Code, sorted by LEVEL.

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.

Multiple Regions with the Same Area Code

Grid with columns Level, City/Region, and Area Code. It shows the regions Yukon,
    Northwest Territories, and Nunavut have the same area code.

To sort by AREACODE and then by REGION:

sort/e "AREACODES.TEL", "AREACODE: a ;REGION: a"
Data Sorted by AREACODE, then REGION

Grid with columns Level, Region, and Area Code, sorted by AREACODE, then REGION