Easytrieve Descending Sort

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.

Progress0 of 0 lessons

D Suffix Syntax

text
1
2
3
4
5
SORT 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.

Per-Key D — Not Global

USING (REGION, BRANCH D, DEPT) interpretation
KeyOrderEffect
REGIONAscendingRegions 01, 02, 10 in low-to-high order
BRANCH DDescendingHighest branch code first within region
DEPTAscendingLow 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.

Major Key D — Whole-File Ranking

text
1
2
3
4
SORT 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.

Minor Key D — Within-Group Ranking

text
1
2
3
4
SORT 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.

Descending Dates and Timestamps

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.

Character Fields Descending

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.

D on SEQUENCE Without LINE

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.

Descending Versus BEFORE Top-N

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.

Common Descending Mistakes

  • Assuming D applies to all keys after it in the list.
  • Major-key D when CONTROL expects grouped major ascending keys.
  • Mixing up USING (DEPT, GROSS D) with USING (GROSS D, DEPT).
  • Forgetting D on SEQUENCE when USING had D—report order differs from file order.
  • Descending signed numeric without signed DEFINE—order surprises.

Testing Descending Keys

  1. Three employees same DEPT different GROSS—verify highest prints first with GROSS D.
  2. Toggle D off—confirm order reverses.
  3. Verify CONTROL groups unchanged when only minor key has D.
  4. Compare SORT file order to SEQUENCE report order.
  5. Test date D with known newest record first within account.

Explain It Like I'm Five

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.

Exercises

  1. Write USING (DEPT, GROSS D) and explain order in words.
  2. Contrast major GROSS D with minor GROSS D under REGION.
  3. Add matching SEQUENCE D to a REPORT.
  4. Identify a report that needs date D as minor key.
  5. List one mistake from putting D on the wrong key level.

Quiz

Test Your Knowledge

1. Descending on one key is coded by:

  • Placing D immediately after that field name
  • Adding DESC keyword on SORT
  • Using STOP
  • FILE UPDATE

2. USING (REGION, PAY D) sorts:

  • REGION ascending; PAY highest first within region
  • Everything descending
  • PAY only; REGION ignored
  • RANDOM within region

3. USING (PAY D, EMP#) is appropriate when:

  • Whole file ranks by pay first, highest to lowest
  • Department subtotals are required
  • Only TITLE changes
  • SQL only

4. SEQUENCE BRANCH PAY D means:

  • Branch ascending; pay descending within branch on report spool
  • Both descending always
  • Only PRINT descending
  • Invalid on reports

5. D on REGION and not on BRANCH means:

  • REGION descending; BRANCH ascending within region
  • Both descending
  • Compile error
  • BRANCH ignored
Published
Read time16 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 D descending suffix on sort keysSources: Broadcom Easytrieve 11.6 Language Reference SORT USING, REPORT SEQUENCEApplies to: Easytrieve descending sort with D suffix