Descending sort walks keys backward: highest pay first, newest date first, Z before A for that key level. Easytrieve does not use a separate DESC keyword on the SORT line. You append D immediately after the field name in USING or SEQUENCE. Only that key reverses; neighbors stay ascending unless they too carry D. Beginners often code USING (DEPT, GROSS D) expecting the whole report to flip—it only reverses GROSS within each DEPT group. Others code USING (GROSS D, EMP#) when they wanted departmental subtotals but instead sorted the entire company by salary. This page teaches per-key D semantics, mixed ascending/descending stacks, major-key D versus minor-key D, report SEQUENCE with D, top-earner and latest-transaction patterns, interaction with CONTROL breaks, and mistakes that scramble group logic. Read Ascending sort first if default order is still unclear.
12345SORT PERSNL TO SORTWRK USING (DEPT, GROSS D, EMP#) REPORT TOP-PAY SEQUENCE DEPT GROSS D EMP# LINE DEPT EMP# NAME GROSS
DEPT ascending groups departments 100 then 200. GROSS D lists highest earners first inside each department. EMP# ascending breaks ties on employee number when gross matches. SEQUENCE mirrors the same per-key directions on report spool after PRINT.
| Key | Order | Effect |
|---|---|---|
| REGION | Ascending | Regions 01, 02, 10 in low-to-high order |
| BRANCH D | Descending | Highest branch code first within region |
| DEPT | Ascending | Low dept within equal region and branch |
Adding D after REGION would reverse regions while branch and dept stayed ascending—a different report shape. Document D placement in program headers so maintainers do not remove one D thinking it is redundant.
1234SORT PERSNL TO SORTWRK USING (GROSS D, EMP#) JOB INPUT SORTWRK IF GROSS GE 100000 PRINT TOP-RPT
GROSS D as the major key sorts the entire file by pay highest-first before EMP# tie-breaks. CONTROL on DEPT after this sort will not group departments together unless DEPT was a prior major key—department rows are interleaved by pay rank globally. Use major-key D for top-N company lists; use minor-key D for within-group rankings.
1234SORT PERSNL TO SORTWRK USING (REGION, BRANCH, GROSS D) REPORT R1 CONTROL REGION BRANCH SEQUENCE REGION BRANCH GROSS D
REGION and BRANCH ascending preserve control groups. GROSS D shows highest paid employee first within each branch on detail lines—useful for branch manager review while regional subtotals still accumulate correctly when CONTROL matches major keys.
Newest-first registers code DATE D or TIME D as major or minor key depending on scope. USING (ACCT-NO, POST-DATE D) keeps account groups together with latest posting first—common for statement detail. USING (POST-DATE D, ACCT-NO) sorts all accounts globally by date—common for operational journals. Pick major key to match the reader's mental model.
D on alphanumeric keys reverses collating sequence for that key: Z before A when ascending would be A before Z. Punctuation ordering still follows ALTSEQ tables—test with real names and codes. Do not confuse descending NAME with case-insensitive alphabetical expectations unless your collating table says so.
SEQUENCE HIRE-DATE D may order detail by hire date even when LINE omits HIRE-DATE from print columns. Field must be present in spool at PRINT—populate file field or W field before PRINT. Hidden sort keys are valid; hidden unpopulated keys are not.
Sorting GROSS D then reading until count exceeds N in JOB is simpler than complex BEFORE for some top-ten lists. BEFORE with STOP after fifty SELECTs caps presort volume without descending—different goal. Combine minor-key D with JOB IF when you need top five per department—often requires duplicate logic or multiple passes, not sort alone.
D on a sort key means count down instead of up for that one rule. If you sort kids by height ascending, shortest first. If you add D to height, tallest first—but only for that rule. If you also sort by class name ascending, each class still sticks together; only height order flips inside the class.
1. Descending on one key is coded by:
2. USING (REGION, PAY D) sorts:
3. USING (PAY D, EMP#) is appropriate when:
4. SEQUENCE BRANCH PAY D means:
5. D on REGION and not on BRANCH means: