In DFSORT, the direction of each sort key is either ascending (A) or descending (D). Ascending means low-to-high: the smallest value (or the earliest in collating sequence for character data) comes first in the output. Descending means high-to-low: the largest value (or the latest in collating sequence) comes first. You specify A or D as the fourth value in each key in SORT FIELDS=. Each key can have its own direction—so you can sort by name A–Z (ascending) and by amount largest-first (descending) in the same job. This page explains exactly what A and D do for character and numeric keys, when to choose each, and how to mix them on multiple keys.
Ascending (A) means the sort order goes from small to large. The record with the smallest value in the key appears first; the record with the next smallest appears second; and so on until the record with the largest value appears last. For character (CH) keys, "small" and "large" are defined by the collating sequence—typically EBCDIC on the mainframe. So A comes before B, and 0 comes before 9. The string "ABC" comes before "ABD" and before "XYZ". For numeric keys (PD, ZD, BI, etc.), ascending means numeric order: 1 before 2, 10 before 20, and negative numbers before positive if you have signed data (e.g. -100, -50, 0, 50, 100). Ascending is the most common choice when you want alphabetical order for names or chronological order for dates or amounts from low to high.
Descending (D) means the sort order goes from large to small. The record with the largest value in the key appears first; then the next largest; and so on until the record with the smallest value appears last. For character keys, Z comes before A (in the sense of "later in sequence first"). For numeric keys, 100 comes before 50, 50 before 20, and 20 before 10. Descending is useful when you want the "top" values first—for example, highest sales amount first, or most recent date first (if your date format sorts chronologically), or Z–A order for names. You specify D as the fourth value in that key: SORT FIELDS=(21,4,PD,D).
Every sort key in SORT FIELDS= has four values: position, length, format, and direction. The direction is always the fourth value. So for one key you have (position, length, format, A or D). For multiple keys you have (pos1, len1, fmt1, A or D, pos2, len2, fmt2, A or D, …). Each key is independent: the first key can be A and the second D, or both A, or both D, or any mix. There is no rule that "primary must be ascending"—you choose what your report or downstream process needs.
1SORT FIELDS=(1,20,CH,A)
Sorts by the first 20 bytes, character, ascending. So records are in A–Z (and 0–9) order by that field. Names or IDs in alphabetical order.
1SORT FIELDS=(30,4,PD,A)
Sorts by the 4-byte packed-decimal field at position 30, ascending. The smallest amount comes first, the largest last. Good for "low to high" numeric order.
1SORT FIELDS=(1,10,CH,D)
Sorts by the first 10 bytes, character, descending. So records are in reverse alphabetical order—Z before A. Less common than ascending for names but sometimes used for display or reporting.
1SORT FIELDS=(25,4,PD,D)
Sorts by the 4-byte packed field at 25, descending. The largest value first, smallest last. Typical for "top N" reports (highest amounts first).
A common pattern is to sort by one key ascending and another descending. For example: sort by customer name (ascending) so names are A–Z, and within the same customer sort by amount (descending) so the highest amount for that customer appears first. You would code:
1SORT FIELDS=(1,20,CH,A,21,4,PD,D)
Primary key: bytes 1–20, character, ascending (name order). Secondary key: bytes 21–24, packed decimal, descending (amount high to low within each name). So the output might look like: Customer A with amount 500, Customer A with amount 200, Customer B with amount 300, Customer B with amount 100, and so on. Each key's direction is applied only when comparing records that are equal on the previous key(s).
For character (CH) keys, A and D refer to the collating sequence. Ascending means the character with the lower code (e.g. in EBCDIC) comes first. So blank (X'40') typically comes before letters, and digits 0–9 have their usual order. Descending reverses that. For numeric keys (PD, ZD, BI, FI), DFSORT compares the numeric value. Ascending means the smaller number first; descending means the larger number first. So the meaning of A and D is consistent: A = low-to-high, D = high-to-low, whether the key is character or numeric. The only difference is how "low" and "high" are determined—by character code or by numeric value.
Use ascending (A) when you want: alphabetical order (A–Z), chronological order (oldest first), amounts or counts from smallest to largest, or any "low to high" order. Most sorted reports (e.g. customer list by name, transactions by date) use ascending for the primary key. Ascending is also the natural order for merging with other processes that expect increasing keys.
Use descending (D) when you want: reverse alphabetical (Z–A), most recent first (if date format sorts that way), highest amount or count first, or any "high to low" order. Descending is common for "top 10" or "worst first" reports—e.g. sort by error count descending so the highest count is at the top. You can also use descending on a secondary key to get "within each group, show largest first" without changing the primary order.
Ascending is like lining up from shortest to tallest: the smallest first, the biggest last. Descending is the opposite: tallest first, shortest last. So A = "small first," D = "big first." For words, A means A then B then C (alphabet order). D means Z then Y then X (reverse alphabet). For numbers, A means 1, 2, 3… and D means 100, 50, 20… You get to pick A or D for each part of the sort (e.g. names A–Z, then amounts biggest first).
1. What does A mean in SORT FIELDS=(1,10,CH,A)?
2. What does D mean for a numeric key?
3. Can you have one key ascending and another descending in the same SORT FIELDS?
4. For character data (CH), ascending order typically means what?
5. You want the highest amount first and the lowest last. What do you specify for that key?