Using SORT Expressions

The SORT command (and its variations, including SORT-ITEM and SORT-LABEL) produces reports in which file items are arranged:

  1. According to the data specified by any attribute name

  2. In ascending or descending order

As described earlier, the LIST command generates unsequenced reports in which the items appear in no particular order. It is extremely useful to be able to display file items in sorted order, for instance, by customer name, zip code, state or by numeric values such as dollar figures and dates.

To sort file items in ascending order, include the word BY followed by the name of the desired attribute. To sort items in descending order, use the BY-DSND modifier.

This query sorts customers in ascending order by last name (beginning with A):

>SORT CUSTOMERS BY LAST-NAME

The next query sorts customers in descending order by state (beginning with Z):

>SORT CUSTOMERS BY-DSND STATE

The justification (line 9 in the Attribute Definition item) of the attribute determines the type of sort. If the attribute is left-justified (L), the sort is alphabetical, whereas if the attribute is right-justified (R), the sort is numeric if the data is numeric. Nonnumeric characters are sorted by their ASCII value.

The table below shows the same set of item-IDs sorted in ascending order. The first list is left-justified; the second is right-justified. Notice the difference in the sort order.

Left-Justified

Right-Justified

1

1

10

2

100

10

101

11

10101

20

11

22

110

100

111

101

2

110

20

111

200

200

201

201

22

220

220

222

222

10101

The following topics are presented in this section:

Sorting By Multiple Attributes

Sorting MultiValued Items

See Also

Producing Reports with LIST and SORT

Using the LIST and SORT Commands

Displaying Selected Attributes

Using Selection Expressions

Using Print Limiters

Producing Vertical Listings with LIST-ITEM